Have had this happen once before and still don’t know what causes it, or how to work-around it (and have seen posts online from others that have run into it too).
I am making a patcher for an Applescript app, and want it to repeat if the user grabs the wrong item, but to exit once they pick the right one. When placed by itself in a fresh Applescript document it works flawlessly everytime. But when I put it into the actual app, it has a bad habit of ALWAYS failing on first-try, and then working on the second.
None of the variables are used beforehand, and the script is really simple overall. What causes this type of thing? Is this related to lagginess in Finder, or does running ‘if statements’ within repeats cause some additional issue?
Very interested in bullet-proofing this issue for the future.
Thanks for any help!
repeat
set AppPath to ""
tell application "Finder" to set AppPath to quoted form of POSIX path of (choose file with prompt "Choose" of type {"APPL"} default location (path to applications folder) without invisibles)
if AppPath contains "/MyApp.app/" then
exit repeat
else
beep
end if
end repeat
set bundleIdentifier to "com.apple.iTunes"
repeat
set theApp to choose file with prompt "Choose" of type {"APPL"} default location (path to applications folder)
tell application "Finder" to set applicationID to id of theApp
if applicationID is bundleIdentifier then
exit repeat
else
beep
end if
end repeat
Notes:
¢ without invisibles is not needed, it’s the default
¢ choose file is part of Standard Additions. The Finder is not needed.
That’s good to know about Finder, but even when I remove it the result is the same.
Cannot use the BundleIdentifier, because it is an Applescript app that is community-modified (lots of people in Macports/Homebrew) and may not always have proper bundle attributes. I need to have it selectable.
Is there a way to make ‘If statements’ more robust/have greater priority?
That didn’t fix it, but it did reveal that the choose dialog is fine. It is the “if statement” that is failing the first run. Will try doing in in a shell script and sending the ‘Exit Repeat’ back through osascript. Kinda’ a drag, but glad to know what is causing it.
I really don’t see any reason to trigger the Finder.
Here this cleaned version behaves flawlessly.
I changed the appName so that the script may execute the exit repeat instruction on my machine.
--set AppPath to ""
set path2apps to path to applications folder # call it only once
repeat
set AppPath to quoted form of POSIX path of (choose file with prompt "Choose" of type {"APPL"} default location path2apps without invisibles)
if AppPath contains "/Dashboard.app/" then
exit repeat
else
beep
end if
end repeat
POSIX path of belongs to Standard Additions
choose file belongs to Standard Additions but it’s not the culprit
path to applications folder belongs to Standard Additions
My understanding is that the original version was unhappy with an instruction triggering these three functions of the OSAX Standard Additions while speaking to the Finder.
Since 10.9 (if I remember well) Apple urge us not to do that.
Predefining AppPath was useless because if we select an application name it will be defined by choose file and if we cancel the dialog the instruction supposed to use the variable wouldn’t be executed.
I kept “without invisibles” because, on my machine, the invisibles are always visibles.
If the script is part of a larger script, speaking to the Finder may be useful to make bring the dialog at front.
This alternate version would behave flawlessly :
set path2apps to path to applications folder
repeat
tell application "Finder" to set AppPath to (choose file with prompt "Choose" of type {"com.apple.application-bundle"} default location path2apps without invisibles)
if quoted form of POSIX path of AppPath contains "/Dashboard.app/" then
exit repeat
else
beep
end if
end repeat
I just used “com.apple.application-bundle” to check that it behave well.
Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) mercredi 1 juin 2016 15:44:34
OK EVERYBOY, fresh Protein over here - Forgot to remove the code in a second place in the code below (stupid error ;0). That is a comfort!
Yvan to your point, tried it and yes it works just as well, BUT the focus on a second loop doesn’t hold (even when putting in a “Tell Me” block) when you don’t invoke Finder (drops to the Dock).
Calling Finder, if the first try is wrong, pops backup in front - that is useful.
set path2apps to path to applications folder
set frontApp to (path to frontmost application as string)
repeat
tell application frontApp to set AppPath to (choose file with prompt "Choose" of type {"com.apple.application-bundle"} default location path2apps without invisibles)
if quoted form of POSIX path of AppPath contains "/Dashboard.app/" then
exit repeat
else
beep
end if
end repeat
Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) jeudi 2 juin 2016 10:10:06