APPLESCRIPT: Keycodes double tab and enter not working - applescript

hi I have this code to open up a program and find a documentnumber.
When I physically type the keys on my keyboard it works.
I just need to fix the following code, instead off typing, through code creating the keycodes :
tab
tab
enter
So I made this:
tell application "System Events"
key code 48
key code 48
key code 76
end tell
This should work right?

It should work, but it doesn't seem to. Not sure why that is...
Try this instead:
tell application "System Events"
keystroke tab & tab & return
end tell

Related

Enter value in a webform and push the return key with applescript

I want to enter a value on a webform and then push enter as there is no button to click on the page
I cannot provide any URL as it is a private web tool.
The search box is a simple webform where I enter a value and then push enter to charge the results.
I already wrote the part of the script adding the value in the box, no problem with that. My issue is to find a way for the keystroke return work on the page.
set theValue to "1111111"
to inputByID(theId, theValue)
tell application "Safari"
do JavaScript "document.getElementById('" & theId & "').value = '" & theValue & "'" in document 1
end tell
end inputByID
inputByID("TextField", theValue)
So this works perfectly. I then tried to add a Keystroke return every where I could imagine and I always get the same error message:
Result:
error "Safari got an error: Can’t get keystroke \"
\"." number -1728 from keystroke "
"
I'm not sure if there is anything else to do here. Looking at the Page Source I cannot find any relevant information, that's why I really wanted to go through this road.
To use a keystroke in your scenario, two conditions must exist.
Safari needs to be frontmost and active.
The cursor must be in a text field that can receive input.
The AppleScript code for that would be:
tell application "Safari" to activate
delay 0.5
tell application "System Events" to keystroke return
With System Events, for return, you can also use:
tell application "System Events" to key code 36
Note that the delay commands allow time for the app to be frontmost and active before the keystroke is sent. Adjust as/if necessary.

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: Pasting Clipboard Text into Open/Save Dialog Box

I have many untitled TextEdit files. I'd like to use applescript to save each using, as a name, the text of the top line of each document.
The following will select and copy the first line of a document (not elegant, but it works), but I can't figure out how to paste the clipboard into the save dialog box (and hit "save" afterwards). Can anyone help?
tell application "TextEdit" to activate
tell application "TextEdit"
tell application "System Events" to key code 126 using command down
tell application "System Events" to key code 125 using shift down
tell application "System Events" to key code 8 using command down
end tell
There are 2 ways of doing:
1) the method using GUI scripting: this is what you've started to do. You simulate keyboard events like a user. It is not recommended for mainly 3 reasons: It is usually slow (you need to add delays to leave time for system open window, close them,..). During the script, if user hits key/mouse by mistake, your script will fail. And finally, you're hardly dependent of user interface of the application: if the editor (here Apple with TextEdit) changes something, like a short cut key, your script will no longer work.
Despite that, if you still want to use that way, here is the script that does it for you. I recommend that you add comments as I did (how to remember that key code 8 is 'c' !). I added some extra options to select the path to save (go home folder, enter special path,...). Up to you to use them or not:
tell application "TextEdit"
activate
tell application "System Events"
key code 126 using command down -- command up (cursor at start)
key code 125 using shift down -- shift down (select 1st line)
keystroke "c" using command down -- command C (copy)
keystroke "s" using command down -- open save dialog
delay 0.5 -- to let save as dialog time to open
keystroke "v" using command down -- paste the title from clipboard
-- other options
-- keystroke "h" using {command down, shift down} -- go home directory
delay 0.5
keystroke "g" using {command down, shift down} -- go to dialog
delay 0.5
keystroke "Desktop/Sample" -- path from Documents folder to Sample folder on Desktop
delay 0.5
keystroke return -- close the go to dialog
delay 0.5
keystroke return -- close the save as dialog
end tell
end tell
2) the method using Applescript instructions. It is usually much shorter, more elegant script, much faster to run, and user can't break it during execution. The script bellow does same as script above: It selects the first text row and save the document with that title. Line 1 defines the folder where to save:
set myPath to (path to desktop folder) as string -- path where to save file
tell application "TextEdit"
activate
tell front document
set myTitle to first paragraph
set myTitle to text 1 thru -2 of myTitle -- to remove the return at end of paragraph
save in (myPath & myTitle)
end tell
end tell
I hope it helps

Keystroke Fn (function) key in Applescript

Is there any way to keystroke the Fn key in apple script? Ideally I would like to be able to "press it" twice in order to launch voice typing.
Thanks in advance!
delay 0.5 -- time to release modifier keys if the script is run with a keyboard shortcut
tell application "System Events"
key code 63 -- fn
key code 63
end tell
See Events.h or my website for the key codes.
In an applescript your file path has to be a macpath not a unixpath i.e., separate the directories with a colon (not a forward slash) e.g.,
tell application "Finder"
open folder "Macintosh HD:Users:username:Library:Speech:Speakable Items"
end tell

How to write Applescript to press Escape key?

I am writing an apple-script.
In which I need to write code which means to hit escape button.
I'm not getting how to do it.
Can any one help?
simple,
tell application "System Events"
key code 53
end tell

Resources