使用Applescript打开多个窗口

时间:2014-04-08 15:06:37

标签: windows shell applescript

我正在尝试编写一个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, 790, 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, 790, 1526}
end tell

2 个答案:

答案 0 :(得分:7)

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

下面的代码应该做你想要的,经过多次辛劳和麻烦后到达[AppleScript障碍]课程。一个障碍是VLC实例'主窗口不会马上创建。

评论提供了更多详细信息。

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

请注意,我使用do shell script "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}, {13, 1043}}
set WIN_SIZES to {{790, 519}, {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打开多个 windows ;值得庆幸的是,this answer显示了如何做到这一点(请参阅那里有关怪癖的评论)。

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

set WIN_POSITIONS to {{13, 36}, {13, 544}, {13, 1043}}
set WIN_SIZES to {{790, 519}, {790, 519}, {790, 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

答案 1 :(得分:0)

经过互联网搜索和大量试用后错误我发现这很有效,只要您允许AppleScript编辑器(或自动机或您自己的应用程序,无论您使用哪个)在System Preferences > Security & Privacy > Privacy > Accessibility(OS X 10.9 Mavericks)下控制您的计算机。第一次运行脚本/应用程序时,系统会提示您更改设置。授予访问权限后,第二次运行脚本/应用程序时,窗口将重新定位。

    do shell script "open -n /Applications/VLC.app /path/to/first/file" --edit path
    do shell script "open -n /Applications/VLC.app /path/to/second/file" --edit path

    delay 3 --wait 3s for VLC to open files, increase if necessary

    tell application "System Events"
        set pidList to the unix id of (every process whose name contains "VLC")

        tell (first process whose unix id is item 1 of pidList)
            set the position of the front window to {0, 22} --edit {x,y}
        end tell

        tell (first process whose unix id is item 2 of pidList)
            set the position of the front window to {640, 22} --edit {x,y}
        end tell
    end tell

它并不像mklement0's answer那么聪明,但作为AppleScript的新手,我能理解发生了什么。