I am trying to send a keystroke (command+ shift+ r) to a macOS (Mojave) app called "Dbeaver" every minute as long as DBeaver is the active app. I have tried the following with no effect.
tell application "System Events"
set activeApp to name of first application process whose frontmost is true
if "DBeaver" is in activeApp then
tell application "System Events" to keystroke "r" using {command down, shift down}
end if
end tell
The script works perfectly fine if its simple like the following:
activate application "DBeaver"
tell application "System Events" to keystroke "r" using {command down, shift down}
I do not have the application you referred to but I tested this following AppleScript code using TextEdit.app and it worked. Let me know if you run into any errors or issues
tell application "System Events"
repeat while (exists of application process "DBeaver")
set activeApp to name of first application process whose frontmost is true
if "DBeaver" is in activeApp then
tell its application process "DBeaver"
repeat while frontmost
keystroke "r" using {command down, shift down}
delay 60
end repeat
end tell
end if
end repeat
end tell
You want to avoid using something like a repeat loop, as that will block the app's user interface (for quitting or to just avoid the spinning wheel of death). A relatively easy way to repeat stuff like that is to make a stay-open application, and put your repeating code in the idle handler, which uses a timer - for example:
on idle
tell application "System Events"
set activeApp to name of first application process whose frontmost is true
if "DBeaver" is in activeApp then
tell application "System Events" to keystroke "r" using {command down, shift down}
end if
end tell
return 60 -- do it again in 60 seconds
end
The statements in the idle handler are run when the app is idle; the return value determines the number of seconds before the handler is run again.
Related
Is it possible to get the text of Pages app, from its current cursor position?
My requirement is like, when user type something in "Pages", I have to show suggestions for the word they are typing.
so I want to find out the current or last word, near current cursor position from "Pages" app.
Either by using AppleScript or Accessibility?
Text is not selected.
I am not looking for "Services" also.
For apps other than "Pages", I used Accessibility and appleScript. but for pages I am not finding any way.
I have also tried below AppleScript, but some reason it works perfectly in "Script Editor", but when I use it in my code, it goes to infinite loop.
tell application "Pages"
activate
end tell
tell application "System Events"
tell application "System Events"
key code 123 using {shift down, command down} -- shift-command-left
end tell
tell process "Pages"
keystroke "c" using {command down}
delay 1
tell application "System Events"
key code 124 -- shift-command-left
end tell
set myData to (the clipboard) as text
return myData
end tell
end tell
If I run this AppleScript in my app, it freeze my Mac only, I have to force quit the Mac to stop it.
This works for me using the latest versions of macOS Mojave and Pages
property theApp : "Pages" -- change value to name of any other application (TextEdit)
tell application theApp to activate
delay 3
tell application "System Events"
tell application process theApp
-- Move the insertion point to the beginning of the previous word.
key code 123 using {option down} -- left arrow key while holding option down
delay 0.2
-- Move the insertion point to the end of the next word. (selects the word)
key code 124 using {shift down, option down} -- right arrow key while holding option and shift down
delay 0.2
keystroke "c" using {command down} -- copies selected wprd
delay 0.2
-- Next 2 key code commands attempt to restore cursor location
key code 124 using {option down} -- right arrow key while holding option down
delay 0.2
key code 123 using {option down} -- left arrow key while holding option down
tell current application to set myData to (the clipboard) as text
delay 4
return myData
end tell
end tell
tell application "iTerm" to activate
delay 0.1
tell application "System Events" to tell process "iTerm"
keystroke "Lsj!"
end tell
I want know how can i get the right way to enter some character. keystroke command can't identify case.
This should work
set the clipboard to "Lsj!" as text
tell application "iTerm" to activate
delay 0.1
tell application "System Events" to tell process "iTerm"
keystroke (the clipboard)
end tell
wch1zpink's answer should work.
You can also write out the letters individually if needed
keystroke "l" using {shift down} -- uppercase
keystroke "sj!" -- lowercase
That should work
tell application "iTerm" to activate
delay 0.1
tell application "System Events" to tell process "iTerm"
key code 37 using {shift down}
key code 1
key code 38
end tell
I made a simple AppleScript that copies the layer names from Photoshop and pastes them into Illustrator. It worked fine in Mountain Lion but now it doesn't work properly in Yosemite. It repeats 6 times, but it doesn't seem to activate Illustrator on the first run through. It does activate Illustrator the other 5 times it repeats. Here it is:
repeat 6 times
tell application "Adobe Photoshop CC 2014" to activate
tell application "System Events"
tell process "Photoshop"
keystroke "/" using command down
keystroke "c" using command down
keystroke tab
end tell
end tell
delay 0.3
tell application "Adobe Illustrator" to activate
tell application "System Events"
tell process "Illustrator"
keystroke "v" using command down
keystroke return
keystroke "-"
keystroke space
end tell
end tell
end repeat
end
Thanks for any help!
tell application "System Events" to repeat 6 times
tell process "Photoshop" to repeat until frontmost is true
set frontmost to true
delay 1
end repeat
keystroke "/" using command down
keystroke "c" using command down
keystroke tab
tell process "Illustrator" to repeat until frontmost is true
set frontmost to true
delay 1
end repeat
keystroke "v" using command down
keystroke return
keystroke "-"
keystroke space
end repeat
I have the script like this here:
on run argv
set pid to item 1 of argv
set fileLocation to item 2 of argv
tell application "System Events"
set theprocs to every process whose unix id is pid
repeat with proc in theprocs
set the frontmost of proc to true
using terms from application "Safari"
set theScript to "document.querySelector(\"input[name='fileField']\").click()"
tell application "Safari" to do JavaScript theScript in current tab of first window
end using terms from
tell proc
keystroke "G" using {command down, shift down}
delay 2
key code 51
delay 2
keystroke fileLocation
delay 10
key code 52
delay 5
key code 52
delay 5
end tell
end repeat
end tell
end run
which is trying to do the file upload process on the Safari browser. The main goal is to run the script against the given process id ( as many Safari will be running on the machine).
I have problem here with the line do JavaScript from:
tell application "Safari" to do JavaScript theScript in current tab of first window
this is executing the click call on the different Safari browser, rather than the one given in the pid.
I just want to make sure that the script runs against the given Safari PID.
How can I do that?
I'm trying to automate opening and saving a file in applescript. I can't seem to get consistent results with the save dialog though. Is it possible to change a save dialog to a specific folder in applescript?
This might help you navigate to a folder once the save dialog is raised:
set the clipboard to "/path/to/your/folder"
tell application "System Events" to tell process "SketchUp" -- I'm guessing on SketchUp name
keystroke "G" using {command down, shift down}
delay 1
keystroke "v" using {command down}
delay 1
keystroke return
delay 1
keystroke return
delay 1
end tell
You can do it and keep your clipboard intact, I think. If your save dialog is in TextEdit, if you last saved something to the desktop, for example, the following would change your destination back to Documents. It's easier just to use ⌘+D for that, of course, but you can use substitute pretty much whatever path you need. If you have a path with a folder having non-AppleScript allowable characters in the path (such as quotes), you can escape each with the backslash ("\") character.
tell application "TextEdit"
activate
try
tell application "System Events"
keystroke "g" using {shift down, command down}
do shell script "sleep 0.2"
keystroke "~/Documents"
do shell script "sleep 0.2"
keystroke return
end tell
end try
end tell