Sunday, September 22, 2019

#1 2019-08-14 04:24:35 pm

maweir
Member
Registered: 2014-03-28
Posts: 19

Restarting Applications

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?

Applescript:

--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

Offline

 

#2 2019-08-14 04:49:43 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 553

Re: Restarting Applications

Applescript:


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

Last edited by KniazidisR (2019-08-14 05:11:39 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Online

 

#3 2019-08-14 06:27:12 pm

maweir
Member
Registered: 2014-03-28
Posts: 19

Re: Restarting Applications

Guess I overcomplicated that...  Thank you!

Offline

 

#4 2019-08-15 05:55:16 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11598
Website

Re: Restarting Applications

Still less complicated

Applescript:

tell application "Safari"
   if it is running then
       quit
       delay 0.5
       launch
   end if
   activate
end tell

Last edited by StefanK (2019-08-15 05:56:09 am)


regards

Stefan

Offline

 

#5 2019-08-15 06:17:25 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 553

Re: Restarting Applications

StefanK wrote:

Still less complicated


Yes, but would be better:

Applescript:


tell application "Safari"
   if its running then
       quit
       delay 0.5
   end if
   activate
end tell

Last edited by KniazidisR (2019-08-15 06:19:11 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Online

 

#6 2019-08-15 10:33:13 am

maweir
Member
Registered: 2014-03-28
Posts: 19

Re: Restarting Applications

Even better, thank you both!

Offline

 

#7 2019-08-15 10:36:41 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11598
Website

Re: Restarting Applications

KniazidisR wrote:


Yes, but would be better:



No, it wouldn't, because this could cause a Invalid Connection error


regards

Stefan

Offline

 

#8 2019-08-15 11:08:03 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 553

Re: Restarting Applications

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.

Last edited by KniazidisR (2019-08-15 11:10:40 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Online

 

#9 2019-08-15 10:56:03 pm

TedW
Member
Registered: 2005-11-02
Posts: 51

Re: Restarting Applications

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...

Applescript:

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

Offline

 

#10 2019-08-16 05:02:40 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 553

Re: Restarting Applications

Thanks, TedW. But this can be done easier:

Applescript:


tell application "Safari"
   if its running then
       quit
       repeat while its running
           delay 0.1
       end repeat
   end if
   activate
end tel

OR:

Applescript:


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

Last edited by KniazidisR (2019-08-16 05:29:57 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Online

 

#11 2019-08-16 07:37:46 am

TedW
Member
Registered: 2005-11-02
Posts: 51

Re: Restarting Applications

That is nice. smile

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)