How to indent AppleScript oneliner - applescript

I have an AppleScript oneliner that I would like to indent. But I wonder how to do this.
Here is the oneliner:
tell application "System Events" to tell appearance preferences to set dark mode to not dark mode
Here is how I tried to accomplish it:
tell application "System Events" to
tell appearance preferences to
set dark mode to not dark mode
end tell
end tell
Apperently this won't work.
What am I missing?

If you wish to keep your command as a "one-liner", but divide it over more than one line, then you need to make use of a line continuation character, which, in AppleScript, is denoted by ¬. This can be entered in Script Editor either by pressing ⌥Enter, or by pressing ⌥L.
Then you can split your one-liner like so:
tell application "System Events" to ¬
tell appearance preferences to ¬
set dark mode to not dark mode
You can experiment with placing the line continuation character in different positions to achieve different types of indentation, e.g.:
tell application "System Events" to tell ¬
appearance preferences to set ¬
dark mode to not dark mode
If you want to change your one-liner (referred to as a simple tell command) to what is called a compound tell command—one that ends with end tell—then you should omit the to after each tell you wish to compound:
tell application "System Events"
tell appearance preferences
set dark mode to not dark mode
end tell
end tell

Related

Applescript - Set value of System Preferences dock size slider on MacOS Monterey

I am trying to use AppleScript to change the dock size to a specified value. My OS is MacOS Monterey v12.0 which is likely important.
I am able to get a handle on the appropriate "Dock Size" slider but I cannot work out how to set its value directly.
Given I am in a tell slider block I have tried...
set value to targetValue
set value of value indicator 1 to targetValue
focusing before setting with set focused to true (makes no difference)
What DOES work but IS NOT precise enough for my requirements, is using increment/decrement
repeat while value is less than targetValue
increment
end repeat
repeat while value is greater than targetValue
decrement
end repeat
...but this is very imprecise and ultimately sets the value to a range that's not precise enough for my liking.
My full script is below. I am invoking it from the command line with
$ osascript -s eo /path/to/file/Resize-Dock.applescript 0.3
Resize-Dock.applescript
#!/usr/bin/osascript
on run argv
set targetValue to item 1 of argv
if running of application "System Preferences" then
quit application "System Preferences"
delay 1
end if
tell application "System Preferences"
activate
reveal pane id "com.apple.preference.dock"
delay 1
tell application "System Events"
tell slider 1 of group 1 of window "Dock & Menu Bar" of application process "System Preferences"
set currentValue to value of value indicator 1
log " Dock size value BEFORE = " & currentValue
set focused to true
######## HERE IS WHERE I NEED HELP PLEASE ########
set value of value indicator 1 to targetValue
set currentValue to value of value indicator 1
log " Dock size value AFTER = " & currentValue
end tell
end tell
end tell
if running of application "System Preferences" then
quit application "System Preferences"
end if
end run
PS: Yes I am aware that I have the option of avoiding AppleScript and writing straight to the defaults with something like...
defaults write com.apple.dock tilesize -int 60
killall Dock
However this has the MAJOR drawback that it borks the application badge counts. I've spent much time trying to solve that directly and now I'm just looking to drive the change via AppleScript to specifically avoid this.
Really appreciate any help 🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼
This works for me all by itself in macOS Monterey.
Example AppleScript code:
tell application "System Events" to ¬
tell dock preferences to set its dock size to 0.3
Notes:
The example AppleScript code, shown above, was tested in Script Editor under macOS Monterey with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Note that while tested under macOS Monterey, nonetheless, this should also work in other versions of macOS.
Also works from Terminal as:
osascript -e 'tell application "System Events" to tell dock preferences to set its dock size to 0.3'
Or can be used in a shell script with a #!/usr/bin/osascript shebang using an on run argv handler., e.g.:
Example AppleScript code:
#!/usr/bin/osascript
on run argv
set targetValue to item 1 of argv
tell application "System Events" to tell dock preferences to set its dock size to targetValue
end run

How «Wait until application launch» applescript?

How to write the code correctly?
I run the application Photoshop in the automator
I'm waiting for it to fully load
Then I press 10 times Tab and press Enter.
I've tried that:
enter image description here
Looks like that part doesn't work. Because Tab starts to click before the application is fully loaded. What's wrong? Thanks!
repeat until application launch
delay 0.5 end repeat delay 0.5
Most likely, the OP does not understand the main thing: GUI scripting (in this case, sending 10 tabs, and then Enter, that is, keystroke tab and keystroke return in AppleScript language) only works with the frontmost window. And the launch command launches an application without bringing its window to the front.
The correct approach is 1) use the activate application "Photoshop" command 2) use the make new document command, 3) check if the new window exists, 4) send keystroke commands. In the Automator, the Run AppleScript action should be something like this:
on run {input, parameters}
tell application "Photoshop"
activate
make new document with properties {name:"myNewDocument"}
repeat until window "myNewDocument" exists
delay 0.1
end repeat
end tell
tell application "System Events"
repeat 10 times
delay 0.1
keystroke tab
end repeat
keystroke return
end tell
return input
end run
NOTE: not tested, because PhotoShop.app is not installed on my Mac. I am ready to correct my script, if needed. In general, the question is not quite clear.
I don't know much about Photoshop, but I know that it has a loading screen. I tried the following code in Affinity Photo which is a similar product to Photoshop.
tell application "Photoshop"
launch
set theBool to false
repeat until theBool
tell application "System Events" to ¬
if menu item "Close" of ¬
menu 1 of ¬
menu bar item "File" of ¬
menu bar 1 of ¬
application process "Photoshop" exists then ¬
set theBool to true
delay 0.2
end repeat
end tell
The repeat until theBool checks if the loading screen is over by checking if some menu item exists which isn't available when the loading screen is open. If the "Close" and the "File" don't work in Photoshop, you may choose something else.
This is the answer:
tell application "Your app"
launch
activate
end tell

Applescript keystroke fails

I have a problem with my application, I want AppleScript to type "date" in Terminal:
(
activate application "Terminal"
tell application "System Events"
keystroke "date"
keystroke return
end tell
)
It works, but if the language of my keyboard is set to Russian, my app types "####" instead of "date". How to make AppleScript always use the English keyboard mapping?
If you have to send a string to the cursor/insertion point, you can avoid the keystroke command by storing the string on the clipboard and then pasting it.
tell application "Terminal" to activate
set theString to "date"
set the clipboard to theString
delay 0.1
tell application "System Events"
tell process "Terminal"
tell menu bar item "Edit" of menu bar 1
click menu item "Paste" of menu 1
end tell
end tell
end tell
delay 0.1
You should also explore sending a command to the terminal window as a command. Tell a Terminal window to do script.
tell application "Terminal"
activate
set thisWindow to do script "echo 'hello world'" in window 1
do script "echo 'goodbye all'" in thisWindow
end tell
You didn't give enough details of what you're doing to know how best to solve your problem.

Using applescript to clear ITerm2 buffer

"Clear buffer" is a menu option under Iterm2's "Edit" menu (command-K) . I'd like to script this to clear Iterm's buffer.
I've tried, based on another site's suggestions,
tell theSession
select
tell application "System Events" to tell process "iTerm2"
click menu item "Clear Buffer" of menu 1 of menu bar item "Edit" of menu
bar 1
end tell
end tell
I've also tried
tell theSession
select
tell application "System Events"
delay 0.1
keystroke "L" using command down
end tell
end tell
Neither seems to do anything. Any ideas?
Tested under macOS 10.13.5 using iTerm2 Build 3.1.7, the default keyboard shortcut for the Clear Buffer command is ⌘K, as shown in the image below.
The following example AppleScript code will activate iTerm, and act on the frontmost window to clear the buffer:
tell application "System Events"
click UI element "iTerm" of list 1 of application process "Dock"
delay 0.25
try
keystroke "k" using command down
end try
end tell
Or use:
tell application "iTerm" to activate
delay 0.25
tell application "System Events"
try
keystroke "k" using command down
end try
end tell
Note that lowercase k is used even though the menu shows an uppercase K. If you have modified the Clear Buffer keyboard shortcut to use ⌘L, then use a lowercase l.

Applescript - Bring window to foreground

I have an application with several windows opened at the same time.
I'd like to bring a specific window to foreground (I know its title).
At the moment I'm using a combination of keys to achieve this task but I'd like to try something different since I'm experiencing some problems with this approach.
tell application "System Events"
set frontmost of process "appIT" to true
keystroke "1" using command down
delay 0.2
end tell
This is possible by using the "AXRaise" action, except on certain window (applications that use X11 for example).
Try this.
set theTitle to "some title"
tell application "System Events"
tell process "appIT"
set frontmost to true
perform action "AXRaise" of (windows whose title is theTitle)
end tell
end tell
If your application is scriptable and allows setting the index of a window, you can do the following (based on an answer in How do I make a Safari window active using AppleScript (elegantly)?)
to raiseWindow of theApplicationName for theName
tell the application named theApplicationName
activate
set theWindow to the first item of ¬
(get the windows whose name is theName)
if index of theWindow is not 1 then
set index to 1
set visible to false
set visible to true
end if
end tell
end raiseWindow
The toggling of the visibility is necessary to deal with some weirdness that occurs with switching applications. If you don't toggle the visibility, the window won't be the first when you switch away from and back to the application. Unfortunately, this toggling shrinks the window to the dock then restores it, a very dramatic UI disruption.
Here's another way I've found to deal with the weirdness:
to raiseWindow2 of theApplicationName for theName
tell the application named theApplicationName
activate
set theWindow to the first item of ¬
(get the windows whose name is theName)
if the index of theWindow is not 1 then
set the index of theWindow to 2
tell application "System Events" to ¬
tell application process theApplicationName to ¬
keystroke "`" using command down
end if
end tell
end raiseWindow2
I don't think System Events can change the front window of a process. Of course you can close the front window until the window you want is on top. That's not really a solution though as you probably don't want to close windows. Really though the only way you could achieve this is if the application itself is apple-scriptable and allows you to do this.

Resources