Modules APPLESCRIPT

?

AppleScript Language Guide

AS Commands Reference 2

À la découverte d'AppleScript

Jean-Philippe Moreux et Aurélien Gaymay

Débutez avec AppleScript
global appname on activeSystemEvent() tell application "System Events" if not (exists process "System Events") then launch delay 2 end if end tell tell application "System Events" to set appname to the name of the current application end activeSystemEvent
global appname on run tell application "System Events" to set appname to the name of the current application alert("Hello world") end run on alert(msg) tell application appname to activate display dialog msg (* 1) display dialog "Titre" default answer "Hello world" buttons {"Annuler", "Remplace"} default button 2 with icon note copy the result as list to {the rbouton, the rstring} display dialog rbouton & " - " & rstring 2) with timeout of (3600) seconds display dialog "XX" end timeout 3) display dialog "Continue le même traitement ?" giving up after 10 -- stop après 10 secondes *) end alert
on changeCaseOfText(theText, theCaseToSwitchTo) if theCaseToSwitchTo contains "Lowercase" then set theComparisonCharacters to "ABCDEFGHIJKLMNOPQRSTUVWXYZ" set theSourceCharacters to "abcdefghijklmnopqrstuvwxyz" else if theCaseToSwitchTo contains "Uppercase" then set theComparisonCharacters to "abcdefghijklmnopqrstuvwxyz" set theSourceCharacters to "ABCDEFGHIJKLMNOPQRSTUVWXYZ" else return theText end if set theAlteredText to "" repeat with aCharacter in theText set theOffset to offset of aCharacter in theComparisonCharacters if theOffset is not 0 then set theAlteredText to (theAlteredText & character theOffset of theSourceCharacters) as string else set theAlteredText to (theAlteredText & aCharacter) as string end if end repeat return theAlteredText end changeCaseOfText
on concat(arr) -- alert(concat({"hello", " ", "world"})) -- "hello world" set r to "" repeat with i in arr set r to r & i end repeat return r end concat
property listeOptions : {¬ "Option A", ¬ "Option B", ¬ "Option C", ¬ "Option D", ¬ "Option E"} on run set resultat to createDialog(listeOptions) if (resultat = false) then return -- "ESC" if (resultat = "Quitter") then return display dialog resultat end run on createDialog(options) choose from list options with prompt "Titre" OK button name "OK" cancel button name "Quitter" copy the result as list to {the r} return r end createDialog
set x to do shell script "ls /Volumes/folder" set x to do shell script ("rsync -av " & source & " " & destination)
global r on run set res to getMenu("Finder") display dialog res end run on getMenu(vApp) set r to "" tell application "System Events" tell process vApp -- Remplacez par le nom de l'application set menuNames to name of every menu bar item of menu bar 1 repeat with menuName in menuNames set r to r & "Menu : " & menuName & return -- log "Menu : " & menuName tell menu bar item menuName of menu bar 1 tell menu menuName my listMenuItems(it, 1) end tell end tell end repeat end tell end tell return r end getMenu on listMenuItems(theMenu, level) tell application "System Events" repeat with mi in menu items of theMenu set indentText to "" repeat level times set indentText to indentText & " " end repeat set r to r & indentText & name of mi & return -- log indentText & name of mi if exists menu of mi then my listMenuItems(menu of mi, level + 1) end if end repeat end tell end listMenuItems
on getClipboard(str) return the clipboard end getClipboard
on run getPositionFenetre() setPositionFenetre() end run on getPositionFenetre() -- tell application "Finder" to set rect_screen to get bounds of window of desktop -- Bureau -- 2560 - 1440 tell application "Finder" activate set rect_screen to get bounds of front window end tell set positionTop to item 1 of rect_screen set positionGauche to item 2 of rect_screen set windowWIDTH to item 3 of rect_screen set windowHEIGHT to item 4 of rect_screen -- display dialog ("Haut : " & (positionTop as string) & return & "Gauche : " & (positionGauche as string) & return & "width : " & (windowWIDTH as string) & return & "Hauteur : " & (windowHEIGHT as string)) end getPositionFenetre on setPositionFenetre() tell application "Finder" activate set fw to front window end tell set haut to 200 set gauche to 100 set bas to 1200 set droite to 600 set bounds of fw to {gauche, haut, droite, bas} end setPositionFenetre
on indexOf(s, f) -- string, find -- alert(indexOf("hello world", "world")) -- 7 return offset of f in s end indexOf
on run set art to "Artiste" set alb to "2026 Album" makeFolder(art, alb) end run on makeFolder(art, alb) do shell script "mkdir -p ~/Desktop/" & quoted form of (art & "/" & alb) as string -- Resultat : Desktop/Artiste/2026 Album end makeFolder
on replaceString(str_text, str_find, str_rep) set text item delimiters to str_find set x to every text item of str_text set AppleScript's text item delimiters to str_rep return x as string end replaceString
on saveClipboard(str) set the clipboard to str end saveClipboard
tell application "System Events" delay 0.2 tell process "Finder" set frontmost to true click menu item "Tout sélectionner" of menu "Édition" of menu bar 1 end tell end tell
on run tell application "TextEdit" to activate sendKey("cmd", "n") sendKey("kst", "Hello world") end run on sendKey(vOpt, theKey) tell application "System Events" if vOpt = "" then key code theKey else if vOpt = "kst" then keystroke theKey else if vOpt = "asc" then keystroke (ASCII character theKey) else if vOpt = "num" then keystroke (ASCII number theKey) else if vOpt = "cmd" then keystroke theKey using {command down} else if vOpt = "opt" then keystroke theKey using {option down} else if vOpt = "shift" then keystroke theKey using {shift down} else if vOpt = "ctl" then keystroke theKey using {control down} else if vOpt = "cmdShift" then keystroke theKey using {command down, shift down} else if vOpt = "cmdAlt" then keystroke theKey using {command down, option down} else if vOpt = "cmdCtrl" then keystroke theKey using {command down, control down} else if vOpt = "cmdOptShift" then keystroke theKey using {command down, option down, shift down} end if end tell delay 1 end sendKey
(* ======================================= ! ! Nom ! ! version 0.x - xx/xx/20xx ! ! Role ! ======================================= *) property test : "test" global appname -- ======================================= on run tell application "System Events" to set appname to the name of the current application repeat with i from 0 to x end repeat end run
on substring(s, d, e) -- string,start,end -- alert(substring("hello world !", 2, 4)) -- ell return characters d thru e of s as string end substring
on toLowerCase(s) -- alert(toLowerCase("ABCD")) -- "abcd" set r to "" repeat with i from 1 to length of s set car to character i of s set num to the ASCII number of car if num > 64 and num < 91 then set car to ASCII character (num + 32) set r to r & car end repeat return r end toLowerCase
on toUpperCase(s) -- alert(toUpperCase("abcd")) -- "ABCD" set r to "" repeat with i from 1 to length of s set car to character i of s set num to the ASCII number of car if num > 96 and num < 123 then set car to ASCII character (num - 32) set r to r & car end repeat return r end toUpperCase