MacOS – automating “Reset Advertising Identifier” is it possible

automationmacosprivacyscript

I want to use osascript or some other way to automate "Reset Advertising Identifier" is it possible?

Best Answer

Example #1 - via Googling

Googling I found this Reddit thread titled: I want to write auto reset script for Advertising Identifier.

The script from one of the replies in that thread is below.

NOTE: I did not test this, am merely sharing it as something that may help steer you in the right direction to ultimately getting a workable solution for you problem.

FYI, I created a small script for this. It's not perfect, and doesn't reliably run in the background, but in case it helps anyone:

-- TODO: make this run reliably in the background...
set isRunning to false
if application "System Preferences" is running then set isRunning to true

tell application "System Preferences"
  activate
  reveal anchor "Privacy" of pane "com.apple.preference.security"
end tell

tell application "System Events" to tell process "System Preferences"
  delay 0.3
  click button "Reset Advertising Identifier" of group 1 of tab group 1 of window "Security & Privacy"
  delay 0.2
  click button "Reset identifier" of sheet 1 of window "Security & Privacy"
end tell

if isRunning is false then
  quit application "System Preferences"
end if

Script appears to be AppleScript.

Example #2 - I made using Automator

This is the first script I've ever done using Automator (be kind). I used the record button and clicked through the steps to construct a Workflow.

Automator Workflow

ssautomatorwf1

Once I made he workflow I copy/pasted it into ScriptEditor.

Automator with steps

ssautomatorwf2

Copy/Paste from Automator → ScriptEditor

ssscripteditorcopypaste

In ScriptEditor I could click the play button to run the script to debug it and see that it was working correctly.

ScriptEditor Play Button

ssscripteditorplaybtn

One I had it working reasonably well, I put it into a Bash shell script so that I could run it standalone from a terminal.

And here's the finished Bash script with the osascript embedded within:

$ cat reset_advertisement_id.sh
#!/bin/bash

osascript <<HERE
-- Click the “Apple” menu.
delay 4.598414
set timeoutSeconds to 2.0
set uiScript to "click menu bar item \"Apple\" of menu bar 1 of application process \"Finder\""
my doWithTimeout(uiScript, timeoutSeconds)

-- System Preferences…
delay 1.185682
set timeoutSeconds to 2.0
set uiScript to "click menu item 4 of menu 1 of menu bar item \"Apple\" of menu bar 1 of application process \"Finder\""
my doWithTimeout(uiScript, timeoutSeconds)

-- Click the “View” menu.
delay 4.124994
set timeoutSeconds to 2.0
set uiScript to "click menu bar item \"View\" of menu bar 1 of application process \"System Preferences\""
my doWithTimeout(uiScript, timeoutSeconds)

-- Security & Privacy
delay 2.306638
set timeoutSeconds to 2.0
set uiScript to "click menu item \"Security & Privacy\" of menu 1 of menu bar item \"View\" of menu bar 1 of application process \"System Preferences\""
my doWithTimeout(uiScript, timeoutSeconds)

-- Click the “Privacy” tab.
delay 4.31466
set timeoutSeconds to 2.0
set uiScript to "click radio button \"Privacy\" of tab group 1 of window \"Security & Privacy\" of application process \"System Preferences\""
my doWithTimeout(uiScript, timeoutSeconds)

-- Click the text “Advertising”
delay 1.997132
set timeoutSeconds to 2.0
set uiScript to "click static text 1 of UI Element 1 of row 12 of table 1 of scroll area 1 of tab group 1 of window \"Security & Privacy\" of application process \"System Preferences\""
my doWithTimeout(uiScript, timeoutSeconds)

-- Click the “Reset Advertising Identifier” button.
delay 1.234389
set timeoutSeconds to 2.0
set uiScript to "click UI Element \"Reset Advertising Identifier\" of group 1 of tab group 1 of window \"Security & Privacy\" of application process \"System Preferences\""
my doWithTimeout(uiScript, timeoutSeconds)

-- Click the “Reset identifier” button.
delay 1.2831
set timeoutSeconds to 2.0
set uiScript to "click UI Element \"Reset identifier\" of sheet 1 of window \"Security & Privacy\" of application process \"System Preferences\""
my doWithTimeout(uiScript, timeoutSeconds)

-- Click the “<fill in title>” button.
delay 1.436771
set timeoutSeconds to 2.0
set uiScript to "click UI Element 6 of window \"Security & Privacy\" of application process \"System Preferences\""
my doWithTimeout(uiScript, timeoutSeconds)

on doWithTimeout(uiScript, timeoutSeconds)
    set endDate to (current date) + timeoutSeconds
    repeat
        try
            run script "tell application \"System Events\"
" & uiScript & "
end tell"
            exit repeat
        on error errorMessage
            if ((current date) > endDate) then
                error "Can not " & uiScript
            end if
        end try
    end repeat
end doWithTimeout
HERE

# REFS
# - https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/AutomatetheUserInterface.html
# - https://apple.stackexchange.com/questions/40944/a-programmatic-method-for-disabling-all-sharing-services
# - https://github.com/herrbischoff/awesome-macos-command-line
# - https://www.macobserver.com/tips/quick-tip/reset-mac-advertising-identifier/
# - https://scriptingosx.com/2018/08/user-interaction-from-bash-scripts/
# - https://www.macworld.com/article/1157370/applescriptsystempreferences.html

To run this:

$ ./reset_advertisement_id.sh

References