一大波 AppleScript 來襲:打開 Chrome 窗口、調整 DevTool 位置、載入 Sublime 項目、執行 ITerm 命令 等等

前言

前陣子說過,發現了 Mac 調度中心的正確用法,在三屏顯示器下很酷,後來又實現了讓 mac 和外聯的顯示器一塊兒隨機成同一個壁紙,真是賞心悅目。html

想要更高效

然而,如何在桌面空間裏爲一堆項目窗口進行自動化佈局這件事一直讓我念念不忘,好在如今有了收穫。web

一大波 Applescript 腳本

此處只分享腳本和用法,具體組裝須要各人自行領會,菜端上來了,怎麼吃,請自行動筷。shell

個人工做環境裏,主要涉及 Chrome Sublime iTerm 等軟件,如下代碼供參考。npm

開一個新窗口(不是新標籤)

方法:api

on newWindow(name, openStr, newWindowStr)
    tell application "Dock"
        activate
    end tell
    tell application "System Events"
        tell process "Dock"
            set frontmost to true
            activate
            tell list 1
                tell UI element name
                    perform action "AXShowMenu"
                    delay 1
                    key code 126 -- up arrow
                    tell menu name
                        try
                            tell menu item openStr
                                perform action "AXPress"
                                -- 第一次打開程序 須要多等幾秒
                                delay 5
                            end tell
                        on error errMsg
                            tell menu item newWindowStr
                                perform action "AXPress"
                            end tell
                        end try
                    end tell
                end tell
            end tell
        end tell
    end tell
end newWindow
複製代碼

用法示例:瀏覽器

newWindow("Google Chrome", "打開", "打開新的窗口")
複製代碼

說明:bash

  • 將經常使用的軟件放置到 Dock 中,右擊的話能夠看到該軟件的菜單,菜單中的文字,便可做爲參數傳入上面給到的方法。
  • 如打開一個新的 google 瀏覽器窗口,這裏有兩個參數,第一個參數是當前沒有 google 瀏覽器進程存在的命令,第二個參數是若是有 google 瀏覽器進程存在的命令。
  • 其餘軟件相似,具體命令你右鍵點一下看文字。

移動窗口到指定位置

方法:session

on moveBounds(name, topLeftX, topLeftY, bottomRightX, bottomRightY)
    tell application name
        set bounds of front window to {topLeftX, topLeftY, bottomRightX, bottomRightY}
    end tell
end moveBounds

on sizePosition(name, topLeftX, topLeftY, width, height)
    tell application "System Events" to tell application process name
        tell window 1
            set {position, size} to {{topLeftX, topLeftY}, {width, height}}
        end tell
    end tell
end sizePosition
複製代碼

用法示例:app

sizePosition("Google Chrome", 40, 15, 1828, 1057)
moveBounds("Google Chrome", 1519, 116, 1919, 874)
複製代碼

說明:svn

  • 這裏提供了兩個方法,注意參數的區別。
  • 之因此有兩種方法,是由於這兩種方法並不是老是生效的,具體軟件具體狀況不一樣,請使用時自行選用。
  • 至於如何取得準確的座標位置,有個簡單方法就是使用各種截圖工具,如snipaste等。

打開 Chrome 的 DevTool 並切換成獨立窗口而後移動到指定位置

方法:

on getFrontWindow()
    tell application "System Events"
        repeat with theapp in (every application process whose visible is true and frontmost is true)
            repeat with ew in (every window of theapp)
                return ew
            end repeat
        end repeat
    end tell
end getFrontWindow

on openChromeDevTool()
    tell application "Google Chrome"
        activate
    end tell
    tell application "System Events"
        key code 53 --esc
        -- 打開開發者工具
        delay 1
        key code 34 using {option down, command down}
    end tell
    -- 切換工具窗口
    delay 3
    set frontWindowName to name of getFrontWindow() as string

    if {frontWindowName does not start with "DevTools"} then
        --display dialog frontWindowName
        tell application "System Events"
            key code 2 using {shift down, command down}
            delay 2
        end tell
    end if
    -- 移動開發者工具
    moveBounds("Google Chrome", 1920, -36, 3410, 1043)
end openChromeDevTool
複製代碼

用法示例:

openChromeDevTool()
複製代碼

說明:

  • 這是我我的的一個特殊需求
  • 在開發移動端網頁時,我經常會獨立 DevTool 的窗口,
  • 因此我用上述代碼實現了窗口獨立並移動到副屏顯示器的需求。

用 Sublime 載入一個項目

方法:

on sublOpenWorkspace(name)
    do shell script "\"/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl\" -n --project \"/Users/AsinW/Library/Application Support/Sublime Text 3/Packages/User/Projects/" & name & ".sublime-workspace\""
end sublOpenWorkspace
複製代碼

用法示例:

delay 1
    newWindow("Sublime Text", "打開", "New Window")
    delay 1
    sizePosition("Sublime Text", 0, 15, 1514, 1080)
    delay 1
    sublOpenWorkspace("jiyingshou_api3")
    delay 3
    sublOpenWorkspace("jiyingshou_admin")
複製代碼

說明:

  • 該示例展現了打開一個新的 Sublime 窗口,移動到指定位置,而後載入多個項目的流程。
  • 注意:在上文的方法裏,使用了 Sublime Text.app 的絕對路徑,請你們自行更改成本身電腦中的對應路徑。
  • 至於如何管理 Sublime 的項目文件,如何生成 sublime-workspace 這樣的文件,請你們自行探索(這是另外一件事了,按理說你們應該已經很早就接觸到了,因此此處不做長篇大論)。

打開 iTerm2 並執行命令

方法:

-- 在當前面板執行命令
on iTermCmdCurrentTab(cmdStrs)
    tell application "iTerm"
        activate
        tell current window
            tell current session
                repeat with cmdStr in cmdStrs
                    write text cmdStr
                end repeat
            end tell
        end tell
    end tell
end iTermCmdCurrentTab

-- 打開一個新標籤,並執行命令
on iTermCmdNewTab(cmdStrs)
    tell application "iTerm"
        activate
        tell current window
            create tab with default profile
            tell current session
                repeat with cmdStr in cmdStrs
                    write text cmdStr
                end repeat
            end tell
        end tell
    end tell
end iTermCmdNewTab


-- 在當前界面橫向再切一個面板出來並執行命令
on iTermCmdInPane(cmdStrs)
    tell application "iTerm"
        activate
        tell current window
            tell current tab
                tell current session
                    split horizontally with same profile
                end tell
                repeat with ss in sessions
                    select ss
                end repeat
                tell current session
                    repeat with cmdStr in cmdStrs
                        write text cmdStr
                    end repeat
                end tell
            end tell
        end tell
    end tell
end iTermCmdInPane

-- 指定面板高度
on iTermSetRows(num)
    tell application "iTerm"
        tell current session of current window
            set rows to num
        end tell
    end tell
end iTermSetRows

-- 激活指定面板
on iTermActiveSession(num)
    tell application "iTerm"
        tell current tab of current window
            select item num in sessions
        end tell
    end tell
end iTermActiveSession
複製代碼

用法示例:

delay 1
        newWindow("iTerm", "打開", "New Window (Default Profile)")

        delay 1
        iTermCmdCurrentTab({"cd /Users/AsinW/svnBox/jiyingshou/web/pc"})
        iTermCmdInPane({"cd /Users/AsinW/svnBox/jiyingshou/web/pc", "npm run dev"})
        iTermSetRows(3)
        iTermActiveSession(0)

        delay 1
        iTermCmdNewTab({"cd /Users/AsinW/svnBox/jiyingshou/web/mobile4"})
        iTermCmdInPane({"cd /Users/AsinW/svnBox/jiyingshou/web/mobile4", "npm run dev"})
        iTermSetRows(3)
        iTermActiveSession(0)

        delay 1
        moveBounds("iTerm", -1187, 136, -1, 935)
複製代碼

說明:

  • 上述示例描述了開啓 ITerm ,並在兩個標籤四個面板中執行對應命令的流程。
  • 在命令執行完最後,該窗口被移到了左側顯示器裏了哦。

後語

工具是死的,人是活的。

有不少你們耳熟能詳的工具或方法,只是聽過,沒有用過,由於你沒有想象到本身使用的場景,等到頓悟的那一天,會發現,哦,能夠這樣那樣的用。

以此記錄,供你們參考或收藏或路過。

原文來自阿星的博客:wanyaxing.com/blog/201808…

相關文章
相關標籤/搜索