为什么 Applescript 在脚本编辑器中运行,但保存为应用程序时出错? [英] Why does Applescript run in Script Editor, but error when saved as Application?

查看:23
本文介绍了为什么 Applescript 在脚本编辑器中运行,但保存为应用程序时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

当我从 AppleScript 编辑器运行它时它显示名称:AppleScript 编辑器",当我将它保存为一个名为 NewTest 的应用程序时它显示名称:NewTest".

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.

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

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."

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

What is different about running in script editor and saving as application under 10.5?

推荐答案

这是另一个想法,虽然我还没有检查.可能导致问题的一件事是命令get".通常,当您运行我的名字"之类的命令时,命令 get 是隐含的,因此您实际上是在运行获取我的名字".问题在于隐含的get"并不总是如此.所以有时你必须明确地说get".每当我遇到像您一样的问题时,我尝试的第一件事就是在命令中添加get"……这已成为习惯,因为您永远不知道.请注意,您始终可以使用 get 一词,而永远不会遇到该问题.因此,尝试将您的命令更改为将 my_name 设置为(获取我的姓名)".我很想知道这是否能解决您的 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.

我浏览了一些旧的脚本.我使用以下代码来获取名称.在我的笔记中,我有这些评论...

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

-- 它是使用路径完成的,因为当从脚本菜单运行脚本时,您将 set myName 写入我的名称,结果是applescript runner"而不是实际名称

-- 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

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

-- 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

这篇关于为什么 Applescript 在脚本编辑器中运行,但保存为应用程序时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆