Why does Applescript run in Script Editor, but error when saved as Application?
My applescript needs to detect its own filename, and the following runs fine on Snow Leopard (10.6)
set my_name to name of me as string
display dialog "Name: " & my_name
It displays "Name: AppleScript Editor" when I run it from AppleScript Editor, and it displays "Name: NewTest" when I save it as an application called NewTest.
When I run it on a Leopare (10.5) machine, it complai开发者_Python百科ns "Can't make name of <> into type string." When I remove the "as string" portion, it runs under Script Editor, returning "Name: Script Editor", but when saved as an application, it errors and says, "Can't get name."
What is different about running in script editor and saving as application under 10.5?
Here's another thought although I haven't checked. One thing that can cause problems is the command "get". In general when you run a command like "name of me" the command get is implied so you're really running "get name of me". The problem is that the implied "get" is not always the case. So sometimes you have to explicitly say "get". Whenever I have a problem like yours the first thing I try is to add "get" to the command... it's become habit because you just never know. Note that you can always use the word get and never have that issue. As such, try changing your command to "set my_name to (get name of me)". I'd be interested to know if that fixes your 10.5 problem. Also note that a name is already a string so there's no need to coerce the result to a string.
EDIT: I looked through some of my older scripts. I used the following code to get the name. In my notes I have these comments...
-- this will get the name of the application or script without any file extension
-- it is done using the path because when a script is run from the script menu, and you write set myName to name of me, then the result is "applescript runner" instead of the actual name
-- also it assures that you're getting the name as it appears in the Finder because sometimes the system events process name is different than the Finder name
on getMyName()
set myPath to path to me as text
if myPath ends with ":" then
set n to -2
else
set n to -1
end if
set AppleScript's text item delimiters to ":"
set myName to text item n of myPath
if (myName contains ".") then
set AppleScript's text item delimiters to "."
set myName to text 1 thru text item -2 of myName
end if
set AppleScript's text item delimiters to ""
return myName
end getMyName
An Applescript application isn't an "application" in the truest sense of the word. A lot of contexts change, like "get path to me" will be different when run as a script or as an application, because they are still good ol' wonky Applescript as opposed to a Carbon or Cocoa-based application. Running similar code against the Finder...
tell application "Finder"
set my_name to name as string
display dialog "Finder: " & my_name
end tell
...behaves as expected because the Finder is a Carbon/Cocoa-based application.
I don't have a real answer other than to say it sounds like there was a change made to the OS relative to the Applescript framework in 10.6 that makes the call to "me" behave more as expected.
I would recommend reading the section in the Applescript guide about the me
and it
keywords to gain more insight into how me
works.
精彩评论