Applescript "do shell script" pros and cons

Are there significant advantages or disadvantages between including shell scripts in an AppleScript and using AppleScript commands?


I rarely use AppleScript so am a complete novice but wanted to save a script as an application to make sure my iTunes media share is available before starting iTunes. I've used "do shell script" to open iTunes but only because I couldn't get it to work properly otherwise.


tell application "Finder"
	if (exists POSIX file "/Volumes/MediaForStreaming") then
		do shell script "/Applications/iTunes.app/Contents/MacOS/iTunes > /dev/null 2>&1 &"
	else
		try
			mount volume "afp://ipAddressOfNAS/MediaForStreaming"
			do shell script "/Applications/iTunes.app/Contents/MacOS/iTunes > /dev/null 2>&1 &"
		on error
			display dialog "Can't mount media share on NAS."
		end try
	end if
end tell


It seems to do what I want although I've not tried to run it from anything other than Mojave nor as another user yet (it has to run on El Capitan as well). I've intentionally not included credentials in the mount parameters as I want it to use the credentials of the user running it.

iMac Line (2012 and Later)

Posted on Dec 14, 2020 5:56 PM

Reply
Question marked as Top-ranking reply

Posted on Dec 14, 2020 9:01 PM

I said in my original post that this script does what I want but have since found problems.


Running it from Script Editor it mounts the share and opens iTunes without any noticeable delay but if I save it as an application and double click on it, it takes several seconds to open iTunes and during that time the system is effectively unresponsive. I can move the cursor with the mouse but trying to do anything else, even moving windows, does not work.


The other shortcoming, which is less of a problem but is annoying, is that it doesn't bring iTunes into focus after running it so it ends up behind other apps I've been using.


I've found how to open an app with AppleScript and think I've solved both problems by replacing

do shell script "/Applications/iTunes.app/Contents/MacOS/iTunes > /dev/null 2>&1 &"

with

tell application "iTunes"
	reopen
	activate
end tell

So it seems that there is a big performance hit running the shell script.

6 replies
Question marked as Top-ranking reply

Dec 14, 2020 9:01 PM in response to gakushaburu

I said in my original post that this script does what I want but have since found problems.


Running it from Script Editor it mounts the share and opens iTunes without any noticeable delay but if I save it as an application and double click on it, it takes several seconds to open iTunes and during that time the system is effectively unresponsive. I can move the cursor with the mouse but trying to do anything else, even moving windows, does not work.


The other shortcoming, which is less of a problem but is annoying, is that it doesn't bring iTunes into focus after running it so it ends up behind other apps I've been using.


I've found how to open an app with AppleScript and think I've solved both problems by replacing

do shell script "/Applications/iTunes.app/Contents/MacOS/iTunes > /dev/null 2>&1 &"

with

tell application "iTunes"
	reopen
	activate
end tell

So it seems that there is a big performance hit running the shell script.

Dec 15, 2020 3:55 PM in response to Camelot

Camelot wrote:
The first time you run any application, it takes a little longer. Subsequent launches should be faster.


The several seconds I mentioned was subsequent runs, not the initial run. The first time it asked for confirmation to give it permissions to Finder (which I did) so the delay was even longer. Certainly in this case including shell script was not the right way to go.


Camelot wrote:
There are differences between telling an application to 'reopen', 'launch' and 'activate'.

'launch' opens the application in the background, leaving your script/app frontmost. If the app is already running this effectively does nothing.
'activate' opens the application if it's not already open and brings it to the front.
'reopen' is rarely used, but it triggers the app to do whatever it would normally do when it was launched from the Finder, even if it's already open. Few applications actually react to this and just effect a 'launch'.


Thanks for the explanation. I wasn't aware of 'launch' and for some reason 'activate' wasn't working for me. I can't remember whether there was an error or it was just that iTunes didn't open but my first attempt had the line

tell application "iTunes" to activate
end tell

which I substituted with the shell script. I'd also tried 'open' but didn't think of 'launch' ;-)

I came across reopen by chance when checking searching for something else.

Dec 15, 2020 11:00 AM in response to gakushaburu

Everytime that you perform a do shell script there will be some overhead as you are effectively briefly leaving AppleScript, and returning a result. Some one-liner do shell scripts might alternatively take several lines of AppleScript code, and debugging time, and still might not run faster than the do shell script. There is a balance knowing when to do one over another, and what you are attempting to achieve.

Dec 15, 2020 12:00 PM in response to gakushaburu

> if I save it as an application and double click on it, it takes several seconds to open iTunes and during that time the system is effectively unresponsive


The first time you run any application, it takes a little longer. Subsequent launches should be faster.


> it doesn't bring iTunes into focus after running it so it ends up behind other apps I've been using.


There are differences between telling an application to 'reopen', 'launch' and 'activate'.


'launch' opens the application in the background, leaving your script/app frontmost. If the app is already running this effectively does nothing.

'activate' opens the application if it's not already open and brings it to the front.

'reopen' is rarely used, but it triggers the app to do whatever it would normally do when it was launched from the Finder, even if it's already open. Few applications actually react to this and just effect a 'launch'.

Dec 16, 2020 9:48 AM in response to gakushaburu

> I can't remember whether there was an error or it was just that iTunes didn't open but my first attempt had the line...


OK, this one is kinda subtle.


The code:


tell application "iTunes" to activate
end tell


will fail to compile because it's not valid AppleScript.


tell application commands take one of two forms. If you're issuing a single command (such as 'activate'), that command can be on a single line and requires no end tell statement:


tell application "iTunes" to activate


If you're issuing multiple commands to the same target then adding a return after the target name creates a compound statement that requires an end tell to terminate:


tell application "iTunes" 
 activate
end tell


The two models are distinct. If you're issuing a one-line statement (tell application x to activate) then you don't need (and cannot have) a corresponding end tell statement. The compound form (with a return and end tell) can have any number of commands within it, even 1 if that's what you want.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript "do shell script" pros and cons

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.