为什么 Applescript 在脚本编辑器中运行,但保存为应用程序时出错?

发布于 2024-09-12 05:05:24 字数 441 浏览 4 评论 0原文

我的 applescript 需要检测自己的文件名,以下内容在 Snow Leopard (10.6) 上运行良好。

set my_name to name of me as string
display dialog "Name: " & my_name

当我从 AppleScript 编辑器运行它时,它显示“名称:AppleScript Editor”,当我将其另存为文件时,它显示“名称:NewTest”名为 NewTest 的应用程序。

当我在 Leopare (10.5) 机器上运行它时,它抱怨“无法将 <> 的名称转换为字符串类型。”当我删除“作为字符串”部分时,它在脚本编辑器下运行,返回“名称:脚本编辑器”,但当保存为应用程序时,它会出错并显示“无法获取名称”。

在 10.5 下,在脚本编辑器中运行和另存为应用程序有什么不同?

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 complains "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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌能量女王 2024-09-19 05:05:24

这是另一个想法,虽然我还没有检查过。可能导致问题的一件事是命令“get”。一般来说,当您运行“name of me”之类的命令时,会隐含命令 get,因此您实际上是在运行“get name of me”。问题是隐含的“得到”并不总是如此。所以有时候你必须明确地说“得到”。每当我遇到像你这样的问题时,我尝试的第一件事就是在命令中添加“get”......这已经成为习惯,因为你永远不知道。请注意,您始终可以使用“get”一词,并且永远不会出现此问题。因此,尝试将命令更改为“set my_name to (get name of me)”。我很想知道这是否可以解决您的 10.5 问题。另请注意,名称已经是字符串,因此无需将结果强制为字符串。

编辑:
我浏览了一些旧的脚本。我使用以下代码来获取名称。在我的笔记中,我有这些注释...

-- 这将获取应用程序或脚本的名称,没有任何文件扩展名

-- 它是使用路径完成的,因为当从脚本菜单运行脚本时,您编写 set myName以我的名字来说,那么结果是“applescript runner”而不是实际名称

- 它还确保您获得 Finder 中显示的名称,因为有时系统事件进程名称与 Finder 名称不同

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

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
何时共饮酒 2024-09-19 05:05:24

Applescript 应用程序并不是真正意义上的“应用程序”。很多上下文都会发生变化,例如“获取我的路径”在作为脚本或应用程序运行时会有所不同,因为与基于 Carbon 或 Cocoa 的应用程序相比,它们仍然是很好的 ol' wky Applescript。针对 Finder 运行类似的代码...

tell application "Finder"
    set my_name to name as string
    display dialog "Finder: " & my_name
end tell

...行为符合预期,因为 Finder 是基于 Carbon/Cocoa 的应用程序。

我没有真正的答案,只是说听起来好像 10.6 中相对于 Applescript 框架对操作系统进行了更改,这使得对“me”的调用更加符合预期。

我建议阅读Applescript 指南中有关 meit 关键字的部分,以更深入地了解 me代码> 作品。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文