How to make finder run a keyboard shortcut in applescript? - macos

This is my current code:
tell application "Finder"
tell application "System Events" to keystroke "k" using {command down}
end tell
But it won't work.
Any tips?

You do not tell the Finder to tell system events to do stuff.
You need to just make sure the Application that you want to intercept the keystrokes is active. Then tell System events to do the keystroke.
tell application "Finder" to activate
tell application "System Events"
keystroke "k" using {command down}
end tell

Related

AppleScript: send terminal process to background and detach

After starting a long-running process in Terminal.app I want to send the process to the background and then detach it from the terminal so that it doesn't get killed accidentally if the session is closed.
The sequence I am trying to perform is:
Press Ctrl+z.
Type bg Enter.
Type disown Enter.
I converted it to the following AppleScript:
tell application "System Events" to keystroke "z" using {control down}
delay 1
tell application "System Events" to key code 36
tell application "System Events" to keystroke "bg"
tell application "System Events" to key code 36
delay .5
tell application "System Events" to keystroke "disown"
delay .5
tell application "System Events" to key code 36
The problem is that after Ctrl+z is sent the script does not proceed to the next lines unless I interactively type and then press Enter. What did I miss?
This is what works reliably:
tell application "System Events"
tell application process "Terminal"
activate
set frontmost to true
keystroke "z" using {control down}
keystroke "bg"
keystroke return
keystroke "disown"
keystroke return
end tell
end tell

How do I make an applescript that switches tabs one to the right

I have tried using the code;
tell application "Google Chrome"
activate
end tell
tell application "System Events"
keystroke 124 using {command down, option down}
end tell
is there anything wrong with this script because command option arrow normally switches tabs
tell application "Google Chrome"
activate
end tell
tell application "System Events"
key code 124 using {command down, option down}
end tell

Applescript click on save button

I got error; Can’t get button "save" of process "TextEdit".
activate application "TextEdit"
tell application "System Events" to tell process "TextEdit"
keystroke "s" using {command down}
click button "save"
end tell
I was also tring to include like "of window 1" still I can't get this working. Any help will be much appreciated. thanks
why not just talk to the app directly ?
tell application "TextEdit"
tell document 1 to save
end tell
if you must use the GUI you need the correct hierarchy ( but it is best to talk to the app directly)
activate application "TextEdit"
tell application "System Events"
tell process "TextEdit"
keystroke "s" using {command down}
delay 1
click button "Save" of sheet 1 of window 1
end tell
end tell

keystroke "1" using command down - beeps instead of working

I'm writing a simple applescript that should focus an app and click "cmd+1".
This is what I wrote:
tell application "System Events"
tell application process "appname"
--Lobby focus
activate
keystroke "1" using command down
end tell
end tell
But instead of working, there's one beep and the application doesn't even take focus.
How can I solve this?
You can't tell processes to activate. Change it to set frontmost to true:
tell application "System Events"
set frontmost of process "Finder" to true
keystroke "1" using command down
end tell
Or tell the application to activate:
activate application "Finder"
tell application "System Events"
keystroke "1" using command down
end tell
If the application has no open windows, reopen opens a new default window:
tell application "Finder"
reopen
activate
end tell
tell application "System Events"
keystroke "1" using command down
end tell

Keystroke command to Show All Bookmarks in Safari not working

I am trying a simple Keystroke command to Show All Bookmarks in Safari 5.0.2 but it is not working -
tell application "Safari" to activate
tell application "System Events"
keystroke "B" using {option down, command down}
end tell
end
Can anyone suggest me where I may be wrong?
Thanks,
Miraaj
Try a little 'b':
tell application "Safari" to activate
tell application "System Events"
keystroke "b" using {option down, command down}
end tell
The big B translates to Shift+B.

Resources