一大波 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…

相关文章
相关标签/搜索