如何将窗口对象转换为AppleScript中的字符串?

发布于 2025-02-04 23:48:22 字数 546 浏览 4 评论 0原文

我有一个applescript检索应用程序的窗口ID。 示例以下脚本检索勇敢浏览器的窗口ID。

set urls to {"https://google.com"}
tell application "Brave Browser"
    set myNewWindow to make new window
    repeat with theURL in urls
        tell myNewWindow to open location theURL
    end repeat
    delay 0.3
    log myNewWindow
    return class of myNewWindow //comment - returns "window" as a class
end tell

我的目标是将窗口ID转换为字符串,反之亦然。

为什么要转换?

  • 我想将窗口ID保存在MacOS上的UserDefaults中。

注意:此AppleScript在MacOS应用中使用。

I've an AppleScript that retrieves window id of an app.
Example following script retrieves the window id of Brave Browser.

set urls to {"https://google.com"}
tell application "Brave Browser"
    set myNewWindow to make new window
    repeat with theURL in urls
        tell myNewWindow to open location theURL
    end repeat
    delay 0.3
    log myNewWindow
    return class of myNewWindow //comment - returns "window" as a class
end tell

My goal is it possible to convert the window id to a string and vice-versa.

Why conversion?

  • I want to save window id in UserDefaults on macOS.

Note: This AppleScript is used in macOS app.

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

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

发布评论

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

评论(1

画尸师 2025-02-11 23:48:22

老实说,我很难理解为什么应该将ID保留在默认情况下。因为 theID 变量已经将此值存储在整个脚本中。并且在任何时候,您都可以在脚本后期的一个部分中使用变量关闭窗口。我只能假设一件事:您想在本地打开一个窗口,然后将其从远程计算机上执行的其他脚本关闭。

并在勇敢的浏览器中获取窗口ID很容易:

set urls to {"https://google.com"}
tell application "Brave Browser"
    set myNewWindow to make new window
    repeat with theURL in urls
        open location theURL
    end repeat
    delay 0.3
    set theID to (id of myNewWindow) as text --> the ID of window in  text form
end tell

-- write theID value to defaults here

从其他脚本关闭窗口:

-- first, read theID from defaults here

tell application "Brave Browser"
    close window id (theID as integer)
end tell

To be honest, it's hard for me to understand why the ID should be kept in defaults. Because theID variable will already store this value throughout the script. And at any time you can close the window using a variable in a later part of the script. I can only assume one thing: you want to open a window locally and then close it from other script executed on a remote machine.

And getting the window ID in Brave Browser as text is easy:

set urls to {"https://google.com"}
tell application "Brave Browser"
    set myNewWindow to make new window
    repeat with theURL in urls
        open location theURL
    end repeat
    delay 0.3
    set theID to (id of myNewWindow) as text --> the ID of window in  text form
end tell

-- write theID value to defaults here

Closing the window from other script:

-- first, read theID from defaults here

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