You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在调用另一个AppleScript时保留'me'的指代值

解决AppleScript跨脚本获取自身名称的问题

问题原因

你当前的写法存在两个核心问题:

  1. appName是App.scpt内部定义的变量,Shell.scpt无法直接访问该变量,因此会返回missing value。
  2. 通过run appScript执行加载的脚本时,me的上下文会切换到运行中的Shell.scpt,导致获取的是Shell的名称而非App的。

解决方案1:让App脚本直接返回自身名称

修改App.scpt,使其直接返回脚本自身的名称:

-- App.scpt
return name of me

在Shell.scpt中接收返回值:

-- Shell.scpt
set appScriptPath to POSIX file "/Path/To/App.scpt" as alias
set appScript to load script appScriptPath
set appName to run appScript
return appName

解决方案2:通过脚本对象的处理程序获取

这种方式更灵活,适合后续扩展更多功能:
修改App.scpt,定义一个获取名称的处理程序:

-- App.scpt
on getScriptName()
    return name of me
end getScriptName

在Shell.scpt中调用该处理程序:

-- Shell.scpt
set appScript to load script POSIX file "/Path/To/App.scpt"
set appName to getScriptName of appScript
return appName

解决方案3:直接从文件路径解析脚本名称

如果不需要从App.scpt内部获取名称,也可以直接在Shell.scpt中解析脚本文件的名称:

-- Shell.scpt
set appScriptPath to POSIX file "/Path/To/App.scpt" as alias
-- 获取带后缀的文件名(例如"App.scpt")
set appNameWithExtension to name of appScriptPath
-- 去掉.scpt后缀
set AppleScript's text item delimiters to "."
set appName to first text item of appNameWithExtension
set AppleScript's text item delimiters to ""
return appName

内容的提问来源于stack exchange,提问作者Spaghetti Sam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 10:42:35