Applescript click on save button - applescript

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

Related

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

Script Editor hiding when telling a different application to close/hide

Why does the following Applescript hide Script Editor?
tell application "Last.fm" to launch
tell application "System Events" to tell process "Last.fm" to keystroke "h" using command down
This code I found will hide "Last.Fm", but also hides Script Editor. Ideally, I want to replace the keystroke "h" with a keystroke "w" but then I get an error:
The document can’t be closed while the script is running.
Why does the script I wrote effect Script Editor?
I don't have Last.fm, so I tried this:
tell application "TextEdit" to launch
delay 2
tell application "System Events" to tell process "TextEdit" to keystroke "h" using command down
And sure enough, it's true, TextEdit was hidden but so was Script Editor.
Then I tried this:
tell application "TextEdit" to launch
tell application "TextEdit" to activate
delay 2
tell application "System Events" to tell process "TextEdit" to keystroke "h" using command down
TextEdit was hidden, but Script Editor was not. So I would guess that this will help in your code too. Having the target app frontmost appears to be crucial (which makes a certain amount of sense, after all).
What is the functionality you're trying to achieve here? Is this the whole script? You want last.fm to launch, and then, ideally, to close the window?
tell application "System Events" to tell process
Doesn't work because it is highly misleading - it will not actually direct a keystroke to a particular application, the keystroke goes wherever it would go when it's hit regardless of the "tell process" statement, which is annoying.
I don't use last.fm, but either of these works for me:
tell application "TextEdit"
launch
activate
end tell
delay 0.1
tell application "System Events"
keystroke "w" using command down
end tell
or
tell application "TextEdit"
launch
activate
end tell
delay 0.1
tell application "System Events"
click menu item "Close" of menu 1 of menu bar item "File" of menu bar 1
end tell
Note that if you plan to run the script from a key command or the script menu, and don't want it to steal focus to last.fm, you can have it launch, close the window (or hide it), and then return you to the front app when the script was run:
set appPath to the path to the frontmost application
tell application "Finder"
set appName to the name of file appPath
set appName to text 1 thru ((offset of "." in appName) - 1) of appName
end tell
tell application "TextEdit"
launch
activate
end tell
delay 0.1
tell application "System Events"
keystroke "w" using command down
end tell
tell application appName to activate

How to make finder run a keyboard shortcut in applescript?

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

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