Reputation: 451
I'm trying to write a script that runs both as an app and from the command line using osascript. In both cases, I want to pop up a "help" dialog. In the case of the app, it should pop up when I double click the app. In the case of the command-line launch, I want it to pop up when I run the script without arguments (eg: osascript myScript.scpt) . The attached script does not pop up the dialog properly when I double click the app, but it does work from the command line. If I delete just the argv on the first line, and then remove the -- on the second, thereby emulating the existence of argv, it works fine with a double click. That is, the behavior is radically different when I use the supplied argv than it is when I don't. Is this a bug or am I doing something wrong?
on run argv -- if I remove the argv from this line
-- set argv to [] -- and then comment *in* this line, it works fine
getDefaults() -- when I double-click the app
if (count of argv) = 0 then
displayHelp() -- doesn't display on double click when I use "on run argv"
else
processFromCommandLine(argv)
end if
end run
on displayHelp()
display dialog "Help!"
end displayHelp
on processFromCommandLine(argv)
end processFromCommandLine
Upvotes: 1
Views: 272
Reputation:
There's almost certainly a better way, but this works:
on run argv
-- Display help.
if argv = current application or ¬
argv's class ≠ list or argv's length = 0 then
display dialog "Help!"
return
end if
-- Do stuff.
end run
It looks like the direct parameter
for the run handler
is set to...
current application
when a script is launched as an application.
me
when run from Script Editor.
Upvotes: 1
Reputation: 3792
The Script App is quitting with error when you double-click because arg is not assigned to a class you can count, but then you try and get its count. Just wrap it in a try block instead.
on run argv
getDefaults()
try
get (count of argv)
processFromCommandLine(argv)
on error
displayHelp()
end try
end run
Upvotes: 1