Differences

This shows you the differences between two versions of the page.

Link to this comparison view

devwiki:applescript [2016/10/20 01:17] (current)
Line 1: Line 1:
 +====== AppleScript tips ======
 +  * application automation <code applescript>​tell application "​Finder"​ to empty trash</​code>​
 +  * computer operation <code applescript>​
 +property target_URL : "​http://​macscripter.net/"​
 +open location target_URL
 +</​code>​
 +  * system operation <code applescript>​
 +set volume 7
 +beep 5
 +say "​Hello!"​
 +say "​Hello!"​ using "​Zarvox"​
 +</​code>​
 +  * user interaction <code applescript>​
 +display dialog "How are you today?" ​
 + buttons {"​Lousy",​ "​Good",​ "​Great!"​} ​
 + default button "​Great!" ​
 + with icon 1 [with icon note]
 + giving up after 5
 +delay 60 -- //delay 60 seconds
 +display dialog "​Please enter a number:" ​
 + default answer "​5"​
 +</​code>​
 +  * user input automation <code applescript>​
 +tell application "​System Events"​
 +  tell process (path to frontmost application as string)
 +    keystroke "​myPassword"​ & return
 +  end tell
 +end tell
 +</​code>​
 +  * script and shell automation <code applescript>​
 +do shell script "cd ~/​Documents;​ ls" ​
 +</​code>​
 +  * path convertion <code applescript>​
 +-- unix path to applescript path
 +set applePath to POSIX file "/​myPath"​
 +set applePath to POSIX file mySel as Unicode text
  
 +-- apple path to unix path
 +set bashPath to POSIX path of applePath
 +</​code>​
 +  * pipe data from command by bracket <code applescript>​display dialog (do shell script "echo $PATH"​)</​code>​
 +  * text processing
 +
 +====== system preference ======
 +  * change dock size <code applescript>​
 +set dockPrefs to (path to preferences folder from user domain as text) & "​com.apple.dock.plist"​
 +
 +-- get the current location of the dock and use that to toggle your settings
 +tell application "​System Events"​
 +tell property list file dockPrefs to set theLocation to value of property list item "​orientation"​
 +
 +-- set the dock size: a value between 0 and 1
 +tell dock preferences
 +if theLocation is "​bottom"​ then
 +set dock size to 0.1
 +else
 +set dock size to 0.9
 +end if
 +end tell
 +
 +-- set the position: left, right, or bottom
 +tell property list file dockPrefs
 +if theLocation is "​bottom"​ then
 +set value of property list item "​orientation"​ to "​left"​
 +else
 +set value of property list item "​orientation"​ to "​bottom"​
 +end if
 +end tell
 +end tell
 +
 +-- quit and relaunch the dock with the new settings
 +tell application "​Dock"​ to quit 
 +</​code>​
 +  * toggle keyboard function key <code applescript>​
 +tell application "​System Preferences"​
 + activate
 + set current pane to pane "​com.apple.preference.keyboard"​
 +end tell
 +
 +tell application "​System Events"​
 + -- If we don't have UI Elements enabled, then nothing is really going to work.
 + if UI elements enabled then
 + tell application process "​System Preferences"​
 + get properties
 +
 + click radio button "​Keyboard"​ of tab group 1 of window "​Keyboard & Mouse"
 + click checkbox "Use all F1, F2, etc. keys as standard function keys" of tab group 1 of window "​Keyboard & Mouse"
 + end tell
 + tell application "​System Preferences"​ to quit
 + else
 + -- GUI scripting not enabled. ​ Display an alert
 + tell application "​System Preferences"​
 + activate
 + set current pane to pane "​com.apple.preference.universalaccess"​
 + display dialog "UI element scripting is not enabled. Please activate \"​Enable access for assistive devices\""​
 + end tell
 + end if
 +end tell
 +</​code>​
 +====== Keycode of AppleScript ======
 +
 +^ esc | 53 ^ tab | 48 ^ '​ | 12 ^ a | 0 ^ ;​ | 6 |
 +^ F1 | 122 ^ ` | 50 ^ ,​ | 13 ^ o | 1 ^ q | 7 |
 +^ F2 | 120 ^ 1 | 18 ^ . | 14 ^ e | 2 ^ j | 8 |
 +^ F3 | 99 ^ 2 | 19 ^ p | 15 ^ u | 3 ^ k | 9 |
 +^ F4 | 118 ^ 3 | 20 ^ y | 17 ^ i | 5 ^ x | 11 |
 +^ F5 | 96 ^ 4 | 21 ^ f | 16 ^ d | 4 ^ b | 45 |
 +^ F6 | 97 ^ 5 | 23 ^ g | 32 ^ h | 38 ^ m | 46 |
 +^ F7 | 98 ^ 6 | 22 ^ c | 34 ^ t | 40 ^ w | 43 |
 +^ F8 | 100 ^ 7 | 26 ^ r | 31 ^ n | 37 ^ v | 47 |
 +^ F9 | 101 ^ 8 | 28 ^ l | 35 ^ s | 41 ^ z | 44 |
 +^ F10 | 109 ^ 9 | 25 ^ /​ | 33 ^ - | 39 ^ left | 123 |
 +^ F11 | 103 ^ 0 | 29 ^ = | 30 ^ return | 36 ^ up | 126 |
 +^ F12 | ?​ ^ [ | 27 ^ \ | 42 ^ space | 49 ^ down | 125 |
 +^ delete | 51 ^ ] | 24 ^ | ^ enter | 52 ^ right | 124 |
 +
 +====== AppleScript ready-to-use library ======
 +  * http://​macdevelopertips.com/​category/​applescript