Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JavaScript で OS X を自動操作

JavaScript で OS X を自動操作

AppleScript の代替となる JavaScript for Automation (JXA) の説明。
kanazawa.rb meetup 48 発表資料。

Tomokazu Kiyohara

August 20, 2016
Tweet

More Decks by Tomokazu Kiyohara

Other Decks in Technology

Transcript

  1. "QQMF4DSJQU4BNQMF set urls to {"https://www.google.co.jp","http://www.yahoo.co.jp","https://www.apple.com"} set countOfUrls to count urls

    tell application "Safari" activate tell window 1 set firstTab to current tab repeat with i from 1 to countOfUrls set URL of document 1 to (item i of urls) if i = countOfUrls then exit repeat set current tab to (make new tab) end repeat set current tab to firstTab end tell end tell 4BGBSJ׾Ꟛְג
 ةـ׾鷄⸇׃אא
 אך63-׾Ꟛֻ
  2. +9"4BNQMF var urls = [ "https://www.google.co.jp", "http://www.yahoo.co.jp", "https://www.apple.com" ] var

    safari = Application("Safari") safari.activate() var w = safari.windows[0] var firstTab = w.currentTab() for (var i in urls) { w.tabs[0].url = urls[i] if (i == urls.length - 1) { break } var _tab = new safari.Tab() w.tabs.push(_tab) w.currentTab = _tab } w.currentTab = firstTab 4BGBSJ׾Ꟛְג
 ةـ׾鷄⸇׃אא
 אך63-׾Ꟛֻ
  3. -FWFM var safari = Application("Safari") safari.activate() var w = safari.windows[0]

    console.log(w.tabs[0].url()) w.tabs[0].url = "https://www.google.co.jp" 4BGBSJ׾Ꟛְג
 植㖈ך63-׾⿫撑׃
 63-׾刿倜ׅ׷
  4. 4UBOEBSE"EEJUJPOT4BNQMF var app = Application.currentApplication() app.includeStandardAdditions = true app.displayAlert("֮׵٦ה") var

    Hello = function() { try { app.displayDialog("ֿ׿ח׍׻") console.log("כ׹٦") } catch(e) { console.log("ּ׏לְ") } } Hello() Hello() app.displayNotification("ק׿ע׿",{withTitle:"׋ְה׷",subtitle:"ׁע׋ְה׷"})
  5. 4UBOEBSE"EEJUJPOT ˖ ٓ؎ـٓٔ׾铣׮ֲ ˖ ⦐➂涸ח 鋙ִגֶֻץֹ"1* ˖ ؿ؋؎ٕ SFBEXSJUF 

    ˖ ؙٔحفن٦س TFU5IF$MJQCPBSE5PUIF$MJQCPBSE  ˖ ؎ٝةؙٓءّٝ EJTQMBZ%JBMPH/PUJDBUJPOTBZ
  6. EP4IFMM4DSJQU4BNQMF var app = Application.currentApplication() app.includeStandardAdditions = true var stdout

    = app.doShellScript("whoami") console.log(stdout) try { stdout = app.doShellScript("ls NOT_EXIST") } catch (e) { var stderr = e.message console.log(stderr) }
  7. 4ZTUFN&WFOUT4BNQMF var safari = Application("Safari") safari.activate() delay(2) var se =

    Application("System Events") se.keystroke("l", {using:"command down"}) delay(2) se.keystroke("http://www.google.com") delay(2) se.keyCode(76)
  8. 4ZTUFN&WFOUT4BNQMF var se = Application("System Events") var safari = Application("Safari")

    safari.activate() delay(1) var toolbar = se.uiElements["Safari"].uiElements[0].uiElements[4] toolbar.uiElements[1].click() delay(1) toolbar.uiElements[5].click() delay(1) se.keyCode(125) // down arrow delay(1) se.keyCode(76) // enter
  9. ،ؙإءؽٔذ؍ ثؑحؙ؝٦س var se = Application("System Events") if(se.uiElementsEnabled()) { console.log("،ؙإءؽٔذ؍

    OK") } else { console.log("،ؙإءؽٔذ؍ NG") // ⟃♴כءأذي橆㞮鏣㹀ך،ؙإءؽٔذ؍鏣㹀׾Ꟛֻ؝٦س var pref = Application("System Preferences"); var pane = pref.panes.byId("com.apple.preference.security") pane.anchors["Privacy_accessibility"].reveal(); pref.activate() }
  10. ה֮׷⢽ var se = Application("System Events") var app = se.uiElements[TARGET_APP_NAME].uiElements[TARGET_APP_NAME]

    var main_area = app.uiElements[5].uiElements["Box"].uiElements[0] var table1 = main_area.uiElements[0].uiElements[0] var tab = main_area.uiElements["Peers"] var table2 = main_area.uiElements[7].uiElements[0].uiElements[0]
  11. EVNQVJ&MFNFOUT var se = Application("System Events") se.uiElements[TARGET_APP_NAME].entireContents().forEach(function(v, i, a){ console.log(`${Automation.getDisplayString(v)}`);

    console.log(`${v.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