Apple Script to run as bash

j09
New Contributor II

Hello All,

Need your help, i have Apple script which will update the macOS for both Intel and M1 and restart if it's M1 it will prompt for the user password to install macOS. now it's working fine when i run from Apple script editor.

But i need to convert the Apple script to run as bash script so that we can push from JAMF.

=========================================================================================

# Launch software Update preference pane

do shell script "open x-apple.systempreferences:com.apple.Software-Update-Settings.extension"

tell application "System Events"

repeat 60 times

if exists (window 1 of process "System Settings") then

delay 3

exit repeat

else

delay 1

end if

end repeat

if not (exists (window 1 of process "System Settings")) then

return

end if

end tell

# Click "Update Now" or "Restart Now" if present

tell application "System Events"

tell process "System Settings"

repeat 60 times

if exists (button 1 of group 2 of scroll area 1 of group 1 of group 1 of group 2 of splitter group 1 of group 1 of window "Software Update" of application process "System Settings" of application "System Events") then

click button 1 of group 2 of scroll area 1 of group 1 of group 1 of group 2 of splitter group 1 of group 1 of window "Software Update" of application process "System Settings" of application "System Events"

exit repeat

end if

tell application "System Events"

if application process "System Settings" exists then

delay 0.5

else

exit repeat

end if

end tell

delay 1

end repeat

tell application "System Events"

if application process "System Settings" exists then

delay 0.5

else

exit repeat

end if

end tell

delay 1

end tell

end tell

end

7 REPLIES 7

Jaykrishna1
Contributor II

Hello jGan,

To convert the AppleScript into a Bash script, you can use the osascript command-line tool, which allows you to execute AppleScript commands from the command line

j09
New Contributor II

@Jaykrishna1 I can run from CLI as you said, but i want push from Jamf as bash script. 

I think he means it in a way that you can write something like this in your script:

osascript << EndOfScript
some applescript code
EndOfScript

 We are also using this to sometimes include Applescripts into our normal shell scripts

j09
New Contributor II

@Ismere Yes, But i am not good in scripting, and i am not sure how add that osacript this in above apple script😬

Ah ok yeah that is no Problem at all.
You just write your normal script and then add all of your osascript into the Lines between the two EndOfScript keys. Eventually you may wantmto move the open part into the normal shell script area, since you are starting in shell and temporaly switching over to osa. which would be something like this:

#!/bin/bash
open x-apple.systempreferences:com.apple.Software-Update-Settings.extension
osascript << EndOfScript
the rest of your apple script copied straight from your editor
EndOfScript
exit 0

 

j09
New Contributor II

@IsmereThank You. i have did same but i am getting an error, and if i run without sudo system setting is opening. I think script should as current login user 
=========================================================================

The application cannot be opened for an unexpected reason, error=Error Domain=RBSRequestErrorDomain Code=5 "Launch failed." UserInfo={NSLocalizedFailureReason=Launch failed., NSUnderlyingError=0x600003080570 {Error Domain=OSLaunchdErrorDomain Code=125 "Domain does not support specified action" UserInfo={NSLocalizedFailureReason=Domain does not support specified action}}}

===========================================================================

#!/bin/bash
 
open x-apple.systempreferences:com.apple.Software-Update-Settings.extension
 
osascript << EndOfScript
 
tell application "System Events"
 
repeat 60 times
 
if exists (window 1 of process "System Settings") then
 
delay 3
 
exit repeat
 
else
 
delay 1
 
end if
 
end repeat
 
 
if not (exists (window 1 of process "System Settings")) then
 
return
 
end if
 
end tell
 
 
# Click "Update Now" or "Restart Now" if present
 
tell application "System Events"
 
tell process "System Settings"
 
repeat 60 times
 
 
 
if exists (button 1 of group 2 of scroll area 1 of group 1 of group 1 of group 2 of splitter group 1 of group 1 of window "Software Update" of application process "System Settings" of application "System Events") then
 
click button 1 of group 2 of scroll area 1 of group 1 of group 1 of group 2 of splitter group 1 of group 1 of window "Software Update" of application process "System Settings" of application "System Events"
 
exit repeat
 
end if
 
tell application "System Events"
 
if application process "System Settings" exists then
 
delay 0.5
 
else
 
exit repeat
 
end if
 
end tell
 
delay 1
 
end repeat
 
tell application "System Events"
 
if application process "System Settings" exists then
 
delay 0.5
 
else
 
exit repeat
 
end if
 
end tell
 
delay 1
 
end tell
 
end tell
 
end
 
EndOfScript
 
exit 0

 

Scripting OSX posted a good way to run scripts or at least parts of it as a logged in User.
You can read about it following this link :
https://scriptingosx.com/2020/08/running-a-command-as-another-user/