加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用Applescript打开多个窗口

发布时间:2020-12-16 01:48:06 所属栏目:安全 来源:网络整理
导读:我正在尝试编写一个Applescript,在不同的屏幕位置打开三个VLC窗口.该脚本打开三个VLC实例,但将它们一个放在另一个上面(使用窗口1的位置).帮助代码赞赏: do shell script "open -n /Applications/Video/VLC.app"tell application "System Events" activate s
我正在尝试编写一个Applescript,在不同的屏幕位置打开三个VLC窗口.该脚本打开三个VLC实例,但将它们一个放在另一个上面(使用窗口1的位置).帮助代码赞赏:

do shell script "open -n /Applications/Video/VLC.app"
tell application "System Events"
    activate
    set bounds of first window of application "VLC" to {13,36,790,519}
end tell

do shell script "open -n /Applications/Video/VLC.app"
tell application "System Events"
    activate
    set bounds of second window of application "VLC" to {13,544,1027}
end tell

do shell script "open -n /Applications/Video/VLC.app"
tell application "System Events"
    activate
    set bounds of third window of application "VLC" to {13,1043,1526}
end tell

解决方法

@khagler的注释提供了正确的指针:VLC实例必须通过它们的PID(进程ID;在AppleScript中称为unix id)在系统事件上下文中进行区分.

下面的代码应该做你想要的,经过多次努力和麻烦后到达[AppleScript障碍]课程.一个障碍是VLC实例的主窗口不会立即创建.

评论提供了更多细节.

请注意,由于用户界面元素是以编程方式操作的,因此出于安全原因,必须为运行脚本的应用程序授予assistive access.

请注意,我正在使用do shell脚本“open -na VLC.app”启动实例,依赖于已知启动服务的应用程序的位置(如果由于某种原因不起作用,请恢复为指定完整的方法)路径).

# Specify the desired window bounds.
# !! In the "System Events" context,windows do not 
# !! have `bounds` properties,but separate `position` and
# !! `size` properties.
set WIN_POSITIONS to {{13,36},{13,544},1043}}
set WIN_SIZES to {{790,519},{790,519}}

# Launch the VLC instances.
repeat with i from 1 to count of WIN_POSITIONS
    do shell script "open -na VLC.app"
end repeat

# Note:
# Instance-specific manipulation must
# be performed in the "System Events" context,because
# we must distinguish the VLC instances by their
# PIDs (process IDs; called `unix id` in AppleScript).
tell application "System Events"

    # Get the PIDs (process IDs) of all VLC instances.
    set vlcPids to get the unix id of every process whose name is "VLC"

    # Loop over all instance PIDs.
    # !! It is imperative to *continue* to use object specifiers
    # !! with *filters based on the PID* so as to ensure that the
    # !! individual instances are targeted.
    # !! Attempting to store references to these instances in
    # !! variables fails subtly,as evidenced by the "Events"
    # !! tab in AppleScript editor later showing the non-specific
    # !! process "VLC" of application "System Events" specifiers.
    set winNdx to 1
    repeat with vlcPid in vlcPids

        # WAIT for each instance to create its main window,wich
        # sadly,is not available right away.
        # Once created,position it.
        set haveWin to false
        tell (first process whose unix id is vlcPid)
            repeat with i from 1 to 25 # times out after 25 * .2 == 5 secs.
                if (count of windows of it) > 0 then
                    set haveWin to true
                    tell front window of it
                        # !! In the "System Events" context,windows do not 
                        # !! have `bounds` properties,but separate `position` and
                        # !! `size` properties.
                        set position to item winNdx of WIN_POSITIONS
                        set size to item winNdx of WIN_SIZES
                    end tell
                    exit repeat
                end if
                delay 0.2 # no window yet; sleep some and try again
            end repeat
        end tell
        if not haveWin then error "VLC instance " & vlcPid & " unexpectedly did not create a window within the timeout period."

        set winNdx to winNdx + 1
    end repeat

end tell

如何使用Finder完成这项工作:

Targeting Finder改变了方法有两个原因:

>只有一个Finder实例.
>你无法用open -na Finder.app打开多个窗口;值得庆幸的是,this answer显示了如何做到这一点(请参阅那里的评论以获得怪癖).

请注意,以下内容将盲目打开其他Finder窗口.

set WIN_POSITIONS to {{13,519}}

# Sample target locations for the Finder windows.
# Note the use of the "System Events" context to faciliate use of
# POSIX-style *input* paths; note,however,that the paths are
# *stored* as HFS paths so that Finder accepts them.
tell application "System Events"
    set WIN_TARGETS to {?
        path of desktop folder,?
        path of folder "~/Downloads",?
        path of folder "/Library/Audio"}
end tell

set winCount to count of WIN_POSITIONS

# Launch the Finder windows.
tell application "Finder"
    # Create the windows in reverse orders.
    repeat with i from winCount to 1 by -1
        set newWin to make new Finder window
        set target of newWin to item i of WIN_TARGETS
    end repeat
end tell

tell application "System Events"

    set i to 1
    repeat with i from 1 to winCount

        tell window i of application process "Finder"
            # !! In the "System Events" context,windows do not 
            # !! have `bounds` properties,but separate `position` and
            # !! `size` properties.
            set position to item i of WIN_POSITIONS
            set size to item i of WIN_SIZES
        end tell

    end repeat

end tell

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读