苹果脚本oneliner

发布于 2024-09-30 12:05:34 字数 67 浏览 1 评论 0原文

是否可以将 applescript 行合并为一个(在 ruby​​ 中可以使用 ; 来完成)?

Is it possible to join applescript lines into one (in ruby it can be done using ;)?

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

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

发布评论

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

评论(4

夏天碎花小短裙 2024-10-07 12:05:34

并不真地。最多可以做的就是采用一个简单的 if-then 语句并将其变成一行...

if (variable) then 
    return true
end if

...变成...

if (variable) then return true

如果您要包含 osascript 命令在 shell 脚本中,那么多行脚本必须用 -e 分隔...

osascript -e 'if (variable) then' -e 'return true' -e 'end if'

但这就是它的范围。 Applescript 文件不像大多数其他编程语言那样是简单的文本文件(不幸的是),我们必须依靠其专门的编辑器进行行管理。

Not really. The most that can be done is to take a simple if-then statement and make it into one line...

if (variable) then 
    return true
end if

...becomes...

if (variable) then return true

If you were to include the osascript command in a shell script, then multiple line scripts must delimited with -e...

osascript -e 'if (variable) then' -e 'return true' -e 'end if'

But that's about the extent of it. Applescript files aren't straightforward text files like most other programming languages (unfortunately) and we have to rely on its specialized editors for line management.

撩心不撩汉 2024-10-07 12:05:34

这取决于你的代码。

当您使用 AppleScript 进行 GUI 脚本编写时,您通常可以将一堆嵌套的告诉块编写为一行。

例如这些嵌套的告诉块:

tell application "System Preferences"
    activate
end tell

tell application "System Events"
    tell application process "System Preferences"
        tell window "System Preferences"
            tell scroll area 1
                tell button "General"
                    click
                end tell
            end tell
        end tell
    end tell
end tell

它们也可以写为:

tell application "System Preferences" to activate
tell application "System Events" to tell application process "System Preferences" to tell window "System Preferences" to tell scroll area 1 to tell button "General" to click

It depends on your code.

When you use AppleScript for GUI scripting, you can often write a bunch of nested tell blocks as one line.

For example these nested tell blocks:

tell application "System Preferences"
    activate
end tell

tell application "System Events"
    tell application process "System Preferences"
        tell window "System Preferences"
            tell scroll area 1
                tell button "General"
                    click
                end tell
            end tell
        end tell
    end tell
end tell

They could also be written as:

tell application "System Preferences" to activate
tell application "System Events" to tell application process "System Preferences" to tell window "System Preferences" to tell scroll area 1 to tell button "General" to click
晨光如昨 2024-10-07 12:05:34

如果您确实想避免 -e 并将所有内容强制放在一行上,您可以通过 echo 推送所有内容

osascript -e "`echo -e \"tell application \\"MyApp\\"\nactivate\nend tell\"`"

,其中 " 变为 \\" 和换行符变为 \n

If you really want to avoid -e and force everything on one line you can push everything through echo

osascript -e "`echo -e \"tell application \\"MyApp\\"\nactivate\nend tell\"`"

Where " become \\" and newlines become \n.

傾城如夢未必闌珊 2024-10-07 12:05:34

我已将 AppleScript 从块格式重新排列为单行格式,如下所示:

块格式

tell application <application>
  activate
  open location <url>
end tell

单行格式

osascript -e "tell application \"\" to activate & open location \"< ;url>\"";

I have re-arranged AppleScript from a block format, to a single line format as such:

Block format

tell application <application>
  activate
  open location <url>
end tell

Single line format

osascript -e "tell application \"<application>\" to activate & open location \"<url>\"";

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