Using applescript to clear ITerm2 buffer - applescript

"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.

Related

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.

Applescript delay issue

I am testing applescripts that I will use later in my OSX app.
I'm getting a 6 sec delay after the click button command below.
After some research it seems that this is a known issue.
What I find interesting is, if i use the commercial app QuicKeys to perform the same
button click there is no delay, so I assume they found a work around.
Anybody have any ideas?
tell application "System Events"
tell process "Pro Tools"
set frontmost to 1
click button "Track List pop-up" of window 1
-- 6 seconds delay before next command is sent
key code 36 -- return key stroke
end tell
end tell
Was having the same problem and resolved it by enclosing the click causing delay in the ignoring application responses block. Here is a quick summary:
OLD CODE (Causes 6 sec delay)
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
click bt
tell (first menu item whose title is "SBH80") of menu of bt
click
tell menu 1
if exists menu item "Disconnect" then
click menu item "Disconnect"
else
click menu item "Connect"
end if
end tell
end tell
end tell
NEW CODE (No delay)
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
ignoring application responses
click bt
end ignoring
end tell
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "SystemUIServer"
tell (first menu item whose title is "SBH80") of menu of bt
click
tell menu 1
if exists menu item "Disconnect" then
click menu item "Disconnect"
else
click menu item "Connect"
end if
end tell
end tell
end tell
Please check detailed answer in the thread listed below.
Speed up AppleScript UI scripting?
Hope this helps.
It seems click or axpress causes a big delay.
Instead - get position and use a third party shell script to do the clicking. Much Much faster.
using clicclik : https://www.bluem.net/en/mac/cliclick/
put in user library/application support/Click
set clickCommandPath to ((path to application support from user domain) as string) & "Click:cliclick"
set clickCommandPosix to POSIX path of clickCommandPath
tell application "System Events"
tell process "Pro Tools"
set frontmost to 1
tell button "Track List pop-up" of window 1
set {xPosition, yPosition} to position
set x to xPosition
set y to yPosition
end tell
do shell script quoted form of clickCommandPosix & " c:" & xPosition & "," & yPosition
key code 36 -- return key stroke
end tell
end tell

how to use automator to send new tweet?

relating to this post, https://apple.stackexchange.com/questions/70585/applescript-opens-new-window-for-everything-when-run.
I wonder if i can highlight the selected text and run this service, can i have the selected text in the new tweet textbox?
Here's the current codes:
activate application "Tweetbot"
tell application "System Events"
tell process "Tweetbot"
repeat until exists
delay 0.4
end repeat
set frontmost to true
delay 0.2
keystroke "n" using command down
end tell
end tell
http://i.stack.imgur.com/aahdK.png
http://i.stack.imgur.com/pHtkX.png
You can pass the selected text as a variable in Automator and use UI scripting to set the contents of the text field.
on run {input, parameters}
activate application "Tweetbot"
tell application "System Events" to tell process "Tweetbot"
keystroke "n" using command down
set value of text area 1 of scroll area 1 of window 1 to (input as text)
end tell
end run
If you run the script with a shortcut that has other modifier keys than command, try replacing keystroke "n" using command down with click menu item "New Tweet" of menu 1 of menu bar item "Tweet" of menu bar 1.

Applescript - System events - Set default location for "open file" popup window

Im using system events to control a program that does not have a applescript library.
I am therefor using system events to control it.
I have gotten the program to open a pop up window for it Open File interface and I would like to get it to default to a certain location. Is this possible.
So Far I have :
tell application "App Name"
activate
end tell
tell application "System Events"
tell process "App Name"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
tell menu item "Import"
tell menu "Import"
click menu item "XML..."
delay 4
end tell
end tell
end tell
end tell
end tell
end tell
end tell
The pop up window defaults to its own last visited location. I would like it to default to a given file path like /Users/userabc/Documents/abcd.XML
Thanks
If you have the "posix path" of a location and the dialog box open, you can do the following. Note that the location can be a folder or a file path. If it's a file path then that file will be selected and you would then just have to "keystroke return" to close the dialog box and open that file. Good luck.
set theLocation to path to home folder
set posixLocation to POSIX path of theLocation
tell application "System Events"
keystroke "g" using {command down, shift down}
delay 0.5
keystroke posixLocation
delay 0.5
keystroke return
end tell
The only problem with this method is that autocorrect starts filling in as apple script types into the text box and screws everything up. Work around is to copy/paste into from applescript.
The keystroke command doesn't work for inserting characters that can't be inserted with the current input source. And it doesn't work at all with some input sources.
You could also set the value of the text field:
tell application "System Events" to tell (process 1 where frontmost is true)
keystroke "g" using {shift down, command down}
tell window 1
tell sheet 1
set value of text field 1 to "/usr/share/dict/connectives"
click button 1
end tell
click button "Open"
end tell
end tell

Resources