Applescript - 将窗口带到前景

时间:2012-11-01 08:34:45

标签: macos window applescript foreground

我有一个应用程序,同时打开了几个窗口。 我想把一个特定的窗口带到前台(我知道它的标题)。

目前我正在使用组合键来完成这项任务,但我想尝试一些不同的东西,因为我遇到了这种方法的一些问题。

tell application "System Events"
    set frontmost of process "appIT" to true
    keystroke "1" using command down
    delay 0.2
end tell

4 个答案:

答案 0 :(得分:18)

除了在某些窗口(例如使用X11的应用程序)之外,可以使用"AXRaise"操作。

试试这个。

set theTitle to "some title"
tell application "System Events"
    tell process "appIT"
        set frontmost to true
        perform action "AXRaise" of (windows whose title is theTitle)
    end tell
end tell

答案 1 :(得分:6)

如果您的应用程序是可编写脚本的并允许设置窗口的索引,则可以执行以下操作(基于How do I make a Safari window active using AppleScript (elegantly)?中的答案)

to raiseWindow of theApplicationName for theName
    tell the application named theApplicationName
        activate
    set theWindow to the first item of ¬
        (get the windows whose name is theName)
    if index of theWindow is not 1 then
            set index to 1
            set visible to false
            set visible to true
        end if
    end tell
end raiseWindow

切换可见性对于处理切换应用程序时出现的一些奇怪现象是必要的。如果不切换可见性,则当您切换回应用程序时,窗口将不是第一个。不幸的是,这种切换将窗口缩小到底座然后恢复它,这是一个非常戏剧性的UI中断。

这是我发现的另一种处理怪异的方式:

to raiseWindow2 of theApplicationName for theName
    tell the application named theApplicationName
        activate
    set theWindow to the first item of ¬
        (get the windows whose name is theName)
        if the index of theWindow is not 1 then
            set the index of theWindow to 2
        tell application "System Events" to ¬
            tell application process theApplicationName to ¬
                keystroke "`" using command down
        end if
    end tell
end raiseWindow2

答案 2 :(得分:0)

我认为系统事件不能改变进程的前窗。当然,您可以关闭前窗,直到您想要的窗口位于顶部。这不是一个真正的解决方案,因为你可能不想关闭窗口。实际上,你可以实现这一点的唯一方法是,如果应用程序本身是苹果脚本,并允许你这样做。

答案 3 :(得分:-1)

    tell application "TextEdit"                
        activate window "historyclips.rtf"                
        set miniaturized of window "historyclips.rtf" to false                
    end tell                
    delay 0.1                
    -- set uiScript to "click menu item \"historyclips.rtf\" of menu 1 of application process \"Dock\""                
    tell application "System Events"                
        tell process "TextEdit"                
            -- set frontmost to true                
            -- tell application "TextEdit" to activate window "historyclips.rtf"                
            -- select (first window whose name starts with \"historyclips.rtf\")                
            delay 0.1                
            key code 36                
            key code 44                
            key code 30                
            key code 36                
            click menu item "Paste" of menu 1 of menu bar item "Edit" of menu bar 1                
            delay 0.1                
            key code 36                
            -- set the clipboard to (do shell script "echo date ")                
            -- click menu item "Paste" of menu 1 of menu bar item "Edit" of menu bar 1                
            -- delay 0.1                
            -- set the clipboard to (do shell script "date -v -30d +%m/%d/%Y")                
            -- click menu item "Paste" of menu 1 of menu bar item "Edit" of menu bar 1                
            tell application "TextEdit" to make new paragraph of text of document 1 with data (do shell script "echo date:")                
            tell application "TextEdit" to make new paragraph of text of document 1 with data (do shell script "date -v -30d +%m/%d/%Y")                
        end tell                
    end tell                
    tell application "System Events" to keystroke tab using command down                
    tell application "TextEdit"                
        set miniaturized of window "historyclips.rtf" to true                
    end tell                
    -- idk what to do if you have blocks of 
    texts and you need to sort them out, 
    replace /] with list if this even possible without combinating? 
    ok for this use textpad 8.5 remove \r\n; carrage 
    return top of file replace: \] with "\r\n\i. ". 
    and paste it into word, make it list then 
    sort and determine the position 
    of your entry in historyclips.rtf, copy desired 
    clips or list of clips to further 
    edit see algorithm for adding 
    entries into copy paste pro: 
    http://chomikuj.pl/pat00100/Boom+1.8+ML+TNT+full+Mac+legacy/Alfred+2+old+link+*2b+przydatne+skrypty/skrypty+do+przypisania+nolicence                
                    
                    
                    
    

用于将剪贴板历史记录保存到文件的脚本示例。与复制粘贴Pro和Alfred一起使用。简单,无需开发人员ASD,Python等;)