如何查找正在执行的 AppleScript 的文件名
如何找到正在执行的 AppleScript 的名称?
原因:我想创建一个根据文件名更改其行为的脚本。像这样的东西:
if myname is "Joe" then ACTION1
else if myname is "Frank" then ACTION2
else ACTION3
How do I find the name of an executing AppleScript?
REASON: I want to create a script that changes its behavior based on its filename. Something like:
if myname is "Joe" then ACTION1
else if myname is "Frank" then ACTION2
else ACTION3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
获取名字的正常方法是使用“我的名字”。然而,applescripts 是由 applescript runner 运行的,因此当您在脚本上使用它时,您会得到“Applescript Runner”作为名称。如果您将脚本编译为应用程序,那么“我的名字”将起作用。获取脚本名称的唯一方法是获取其路径并提取名称。因此,这样的东西适用于脚本......
The normal way to get the name is by using "name of me". However applescripts are run by applescript runner so when you use that on a script you get "Applescript Runner" as the name. If you compile your script as an application then "name of me" will work. The only way to get the script name is by getting its path and extracting the name. Something like this would thus work for scripts...
以下是适用于以下所有内容的方法:
*.scpt
文件(已编译的 AppleScript 文件;在 AppleScript 编辑器中运行或使用osascript
运行)osascript
运行)#!/usr/bin/env osascript
):*.app
文件*.app
使用 Automator 创建的包含 AppleScript 的文件注意:相比之下,它不适用于以下情况:
*.workflow
文件)的 macOS 服务) - 他们总是报告 使用 Automator 创建的WorkflowServiceRunner[.xpc]
*.workflow
文件,其中包含 ApplesScript 操作并使用automator
运行 - 它们始终报告Automator Runner[.app]
获取正在运行的脚本的名称,包括文件扩展名(
.scpt
、.app
或. applescript
,视情况而定可能是):如果您想使用单个命令删除文件扩展名,请使用以下基于
do shell script
的方法:这是一个更详细的全 AppleScript 替代方案(但 AppleScript 更简洁)标准):
最后,这是一个变体:您可以通过
set myname to getMyName()
来调用一个子例程:Here's a method that works for all of the following:
*.scpt
files (compiled AppleScript files; run in AppleScript Editor or withosascript
)*.applescript
files (uncompiled AppleScript files; run in AppleScript Editor or withosascript
)#!/usr/bin/env osascript
):*.app
files created with AppleScript Editor*.app
files created with Automator that contain AppleScript actionsNote: By contrast, it does not work for the following:
*.workflow
files) - they always reportWorkflowServiceRunner[.xpc]
*.workflow
files created with Automator that contain ApplesScript actions and that are run withautomator
- they always reportsAutomator Runner[.app]
Get the name of the running script, including filename extension (
.scpt
,.app
, or.applescript
, as the case may be):If you want to remove the filename extension with a single command, use the following,
do shell script
-based approach:Here's an all-AppleScript alternative that is more verbose (yet concise by AppleScript standards):
Finally, here's a variation: a subroutine that you can call with
set myname to getMyName()
:查找路径基础部分的更简单方法是使用
name of
:An easier way to find out the base part of the path is using
name of
:也许这个:
将 appname 设置为当前应用程序的名称
Maybe this:
set appname to name of current application