Is there a better way to go about this. For some reason, if I do not close the safari application and restart it before continuing on with my script I run into several errors. Is there a better way to write this?
--If Safari is running close it & repoen it. If it is not running open it
tell application "System Events"
set Application_ID to (unix id of processes whose name is "Safari")
end tell
if Application_ID is not {} then
try
do shell script "kill -15 " & Application_ID
delay 2.0
tell application "Safari" to activate
tell application "System Events"
repeat until window 1 of process "Safari" exists
delay 0.005
end repeat
end tell
end try
else if Application_ID is {} then
tell application "Safari" to activate
tell application "System Events"
repeat until window 1 of process "Safari" exists
delay 0.005
end repeat
end tell
end if
tell application "System Events" to set isRunning to process "Safari" exists
tell application "Safari"
if isRunning then
quit
delay 0.5
end if
activate
end tell
This reason is I put delay 0.5 second. I tested your script too. It requires for some reason bigger delay to avoid this error on my machine. My machine is slow, so to test on your machine, put some smaller delay on both scripts.
This script doesn’t have to guess at the proper delay time; it waits until the app closes, then relaunches it. I’ve put the app name into a variable for generality…
set processName to "Safari"
tell application "System Events"
if exists process processName then
my quitIt(processName)
end if
repeat while (exists process processName)
delay 0.2
end repeat
my launchIt(processName)
end tell
on quitIt(processName)
tell application processName
quit
end tell
end quitIt
on launchIt(processName)
tell application processName
activate
end tell
end launchIt
tell application "Safari"
if its running then
quit
repeat while its running
delay 0.1
end repeat
end if
activate
end tel
OR:
my restart_App("Safari")
on restart_App(appName)
tell application appName
if its running then
quit
repeat while its running
delay 0.1
end repeat
end if
activate
end tell
end restart_App