LoginSignup
1
0

More than 5 years have passed since last update.

簡易WEBサーバ開始・停止toggleをAppleScriptで

Last updated at Posted at 2017-12-30

AppleScriptでsimpleHTTPServerの開始と停止をtoggleする

Finderで最前面のウインドウ(なければDesktop)を配信します。
クリップボードに"http://local〜" をコピーするので、対応バージョンのOSが揃っていればiPhoneですぐに見られます。
Photoshop CC(2018)でデバイスプレビュー機能が削除されたため作成。

terminal-notifierを使っていますが、無ければdisplay alertに置き換えて。
(terminal-notifierはbrewで)

tell application "Finder"
    set myScript to "python -m SimpleHTTPServer 8888" as Unicode text
    set pid to (do shell script "ps ax | grep " & (quoted form of myScript & "$") & " | grep -v grep | awk '{print $1}'")
    if pid is "" then
        set myDir to POSIX path of (insertion location as alias)
        do shell script "(cd " & myDir & " && " & myScript & ") &>/dev/null &"
        set the clipboard to "http://" & IPv4 address of (get system info) & ":8888/"
        do shell script "/usr/local/bin/terminal-notifier -title 'PythonWEBサーバ' -message '" & myDir & "でWEBサーバを開始しました。アドレスをクリップボードにコピーしました。'"
    else
        do shell script "kill " & pid
        do shell script "/usr/local/bin/terminal-notifier -title 'PythonWEBサーバ' -message 'WEBサーバを停止しました。'"
    end if
end tell
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0