1

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

1

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

  • Not sure whether you tested the code to see if the code would work, but the script as coded will absolutely fail under default conditions. Meaning that unless one has previously gone to System Preferences > Security & Privacy > Privacy > Advertising and not since clicked elsewhere within the System Preferences > Security & Privacy path, that is the only possible way as coded it might work. I say might, because with UI scripting the value of the delay command can be a break point and other methods should be use too. There is critical code missing from the script! – user3439894 Apr 19 at 21:37
  • @user3439894 - no I didn't try it out, I'm not familiar w/ AppleScripts. I'm a long term Linux/UNIX person who is digging more into macOS. I tried to characterize this as something I found. Let me add a note stating that it's untested by me so that it's clear to anyone that this is an untested approach that may get you started but may need to be tweaked. – slm Apr 19 at 21:40
  • @user3439894 - I added a disclaimer. – slm Apr 19 at 21:43
  • 1
    JSYK... A critical piece of code that's missing is select row 12 of table 1 of scroll area 1 of tab group 1 of window "Security & Privacy" of application process "System Preferences" and would go before the click button "Reset Advertising Identifier" ... line of code. However, that line at this point in time is only good on macOS Mojave in a default install and may be subject to change on the row number and as such should be coded differently, but with the other poor coding practices in the script, it goes well with it. :) – user3439894 Apr 19 at 22:15
  • @user3439894 - I took a wag at making my own osascript using Automator, lmk how that looks pls. – slm Apr 20 at 3:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .