Application bundle runs from Script Editor, but not as an Application

I wrote a little script for Allison Sheridan, of the NosillaCast podcast, to set up a new episode for her. I decided the easiest way to do it was to tell Finder to open a locked GarageBand file, then begin a save as so she doesn’t have any idea. The script is saved as an application bundle and the file is in the Resources folder, named “NosillaCast Template.band”. The script runs perfectly from Script Editor, but not as a normal application. You double-click it, and the icon starts to animate into the Dock but only gets to about 4x4 pixels before animating out again, without doing anything. Here’s the script text:

set templatePath to (path to me as text) & "Contents:Resources:NosillaCast Template.band"
tell application "Finder" to open (templatePath as alias)

---------------------

set theYear to year of (get current date)

set theMonth to (month of (current date) as integer)

set theDay to day of (get current date)

set saveText to "NC_" & theYear & "_" & theMonth & "_" & theDay

activate application "GarageBand"

tell application "System Events"
	keystroke "S" using command down
	keystroke saveText
end tell

And here is the script file.

Hi,

try this alternative way, it duplicates the template file to the desktop, renames it and opens it


set templatePath to (path to me as text) & "Contents:Resources:NosillaCast Template.band"
set destinationFolder to path to desktop
---------------------
set newFileName to "NC_" & (do shell script "/bin/date +%Y_%m_%d") & ".band"
do shell script "/bin/cp -Rp " & quoted form of POSIX path of templatePath & space & quoted form of (POSIX path of destinationFolder & newFileName)
tell application "GarageBand"
	activate
	open ((destinationFolder as text) & newFileName) as alias
end tell

Here is your original method, to send Apple Events like keystrokes to an application,
the target process must be specified


set templatePath to (path to me as text) & "Contents:Resources:NosillaCast Template.band"
---------------------
set  saveText to "NC_" & (do shell script "/bin/date +%Y_%m_%d")

tell application "GarageBand"
	activate
	open templatePath as alias
end tell

tell application "System Events"
	tell process "GarageBand"
		keystroke "S" using {shift down, command down}
		repeat until exists sheet 1 of window 1
			delay 0.5
		end repeat
		keystroke saveText
	end tell
end tell

The problem is she uses a dynamic folder structure for her podcasts, and I’d like it to end with her being able to navigate to that folder within GarageBand. I have a hard time believing this isn’t’ possible.

Is that really necessary, it was working fine before. Oh well.

I got rid of your delay for the sheet, because it was taking up time and I want her to have as little time as possible to put something in the way of the keystrokes.

Thanks for the date shell script, though. I had no idea that was possible, but then I’m not a shell kind of guy.

The script is now this:

set templatePath to (path to me as text) & "Contents:Resources:NosillaCast Template.band"

set saveText to "NC_" & (do shell script "/bin/date +%Y_%m_%d")

tell application "GarageBand"
	activate
	open templatePath as alias
end tell

tell application "System Events"
	tell process "GarageBand"
		keystroke "S" using {shift down, command down}
		keystroke saveText
	end tell
end tell

The link in the first post is also now updated, thanks to the magic of DropBox.

Is there really no way to have this work? It’s successful when saved as a script bundle and run from the Script menu. This is very annoying, and I might have to implement your solution.

Thanks for everything anyway!

Ok, it suddenly worked and I have no idea why. If you want the script it’s here, and you can adapt it to your use. Thanks StefanK for your help.

Normally if you write and save a script as an .app on your Mac, and then copy that .app over to someone else’s Mac.
It will not work, as the permissions of the file are wrong.

What I normally do (the easiest way) is copy the .app over to their mac , open it in Script Editor, and save and replace the original.
This sets the permissions up for them.

But I note you also added the bit that Stefan told you about, which probably helped.

In Tiger, keystrokes are simply applied to whatever process is frontmost at the time and work even with GUI Scripting disabled. I don’t know what the situation is in Leopard. I think it probably doesn’t hurt, though, to have System Events itself ensure that the required process is frontmost before issuing the keystrokes.