I am trying to send keystrokes to the application VisualBoyAdvance using AppleScript, but I cannot get it to work.
My code, so far, is this:
tell application "VisualBoyAdvance"
activate
tell application "System Events"
keystroke "k"
end tell
end tell
When I tell VisualBoyAdvance directly, I get this error:
error "VisualBoyAdvance got an error: Can’t get keystroke \"k\"." number -1728 from keystroke "k"
I have tried telling VisualBoyAdvance directly, and I have also tried using key code 40, but I still cannot get it to work. Strangely enough, this does work:
tell application "VisualBoyAdvance"
activate
tell application "System Events"
keystroke "d" using {command down}
end tell
end tell
But that is a keyboard shortcut that shows up in the menu bar, so I guess it would be a bit different.
How can I use AppleScript to simulate a keypress and make the application respond to it? If I cannot use AppleScript for this, what else could I use?
I think you're almost there. Here's something I used for Safari; in this example, I send key code 48 (tab).
tell application "Safari"
activate
tell application "System Events" to tell process "Safari" to key code 48
end tell
AFAICS this ought to be largely independent of AppleScript support in the target process as you're asking System Events to simulate the key press via Universal Access.
For help with key codes, see this useful application: http://manytricks.com/keycodes/
It's the developer's choice to make an application fully Applescript aware. Menu items are Applescriptable from the Finder's point of view, but other UI options may or may not be. See UIElementInspector to examine that application for scriptable elements.
I can't garuntee anything as I don't have this app but here is some things to try
tell application "VisualBoyAdvance"
activate
tell application "System Events"
tell application process "VisualBoyAdvance"
try
keystroke "k"
on error
try
keystroke (ASCII character 75)
end try
end try
end tell
end tell
end tell
Related
I try to make a code assembly to create my own applescript to create an image in Photoshop but unfortunately I have an error and I can't find where it comes from.
The first part of the script works, the random image is well copied to the clipboard but the rest is problematic.
-- Select random image
tell application "Finder"
set bg_img to file (random number from 1 to (count files of folder "HD:Works:Club:LAYERS:0-BG")) of folder "HD:Works:Club:LAYERS:0-BG"
end tell
tell application "Preview"
activate
open bg_img
end tell
-- Select and copy image to clipboard
tell application "System Events"
tell process "Preview"
keystroke "a" using command down
keystroke "c" using command down
end tell
end tell
-- Quit Preview Application
tell application "Preview" to quit
tell application "Adobe Photoshop 2022"
-- Create a new document
set docRef to make new document with properties {width:1200, height:1200, resolution:72}
tell docRef
-- Unlock the background layer and fill it with gray color
set background layer of layer 1 of docRef to false
fill selection with contents {class:RGB color, red:200, green:200, blue:200}
end tell
end tell
-- Paste image to new layer
tell application "System Events"
tell process "Adobe Photoshop 2022"
set newLayer to make art layer with properties {name:"LayerA"}
keystroke "v" using command down
end tell
end tell
Ok, a compilation error; I got it now. Try this:
-- Paste image to new layer
tell application "Adobe Photoshop 2022"
activate
set newLayer to make art layer with properties {name:"LayerA"}
end tell
tell application "System Events"
tell process "Adobe Photoshop 2022"
keystroke "v" using command down
end tell
end tell
Remember, you use a tell application "NAME" … end tell block to 1. tell AppleScript which app to send commands to, and 2. the app from which to get the custom terminology (keywords) for commands and objects.
Thus your tell application "System Events"…end tell block uses SE terminology and commands. The tell process "Photoshop"…end tell block inside it is still addressed to System Events. process is an SE-defined keyword for an SE-specific object. Likewise keystroke is an SE-specific command.
To make a new art layer in Photoshop, you have to put that command inside its own tell application block which is targeted at PS.
Yeah, AppleScript is often confusing. And System Events is 10× more confusing than that, thanks to it being not-obvious what is GUI Scripting and what isn’t. Good luck.
--
p.s. I’ve a feeling the tell process block is totally redundant, and is only necessary when using GUI Scripting to manipulate an app’s windows. IIRC the keystroke command doesn’t know or care what app receives those keys, in which case what’s important is that you activate Photoshop before you start “pressing” keys. I’ll leave it to you to determine if you can discard the tell process.
p.p.s. AppleScriptable apps usually don’t include cut and paste commands, usually ’cos they already provide far better ways of getting and setting content. Photoshop, however, does. So if you’ve not already tried it, try inserting a paste command at the end of your “tell Photoshop” block, then comment out the “tell System Events” block, and see if that works for you. If so, you can get rid of that System Events block entirely!
p.p.p.s. It’s also worth investigating PS’s dictionary further, to see if it can open and place your “bg_file” directly. If so, you can eliminate the “tell Preview” and the rest of that System Events horridness too!!
I want to unlock mac programmatically. Currently i am using Apple script for this propose. And it is working all fine but The issue is it is time consuming process. It take around 4-5 seconds to execute the script. Here is the script i am execution using NSAppleScript. Please suggest me how can i make it work fast. or Any other fastest possible way. Thanks
tell me to activate\n
tell application \"System Events\" to keystroke \"PASSWORD\"\n
tell application \"System Events\" to keystroke return\n
tell application \"System Events\" to keystroke return"
tell application "System Events"
tell security preferences
set require password to wake to false
end tell
end tell
tell application "System Events"
stop current screen saver
end tell
tell application "System Events"
tell security preferences
set require password to wake to true
end tell
end tell
I am trying to simulate pressing the key 1 within a Java application. I have tried using key code 18, text to type, key down and keystrokes but whatever I try, the application displays the a character. Even when the script types into applescript window I just get aaaaaaa.
What am I doing wrong and how to properly simulate keypresses with AppleScript?
Did you try anything like the following?
tell application "System Events"
keystroke "1"
end tell
You can pass string literals to System Events keystroke-Handler
I need an applescript to open safari in full screen an to hide the toolbar on mavericks.
it sounds easy but it isnt!
i need to open safari then open google in full screen mode an then hide the toolbar.
it would be the equivilent to the below sample but instead for safari
tell application "Google Chrome"
open location "http://internet.ceo.wa.edu.au/Pages/default.aspx"
end tell
tell application "Google Chrome" to activate
tell application "System Events"
keystroke "f" using {command down, shift down}
end tell
Could be simple as this:
tell application "Safari"
activate
if (count of windows) is 0 then -- Remove "if" statement if you don't want to make a new window if there is none
make new document at front
end if
tell application "System Events" to keystroke "f" using {command down, control down}
end tell
Not sure if you can make it with no toolbar at all.
Update 4/4
not sure what you can do with it but look into this program. If it works the way you want. Add a system events to use the drop downs to select the item.
The Barbarian Group has a freeware app called "Plainview", which seems to be just a wrapper around Webkit. It works as a "Fullscreen kiosk-style presentation content viewer", similar to what Chrome presentation mode does.
Anyways, it's a free download, so no risk in trying. Scroll almost to the bottom of this page:
http://barbariangroup.com
Direct download:
http://s3.amazonaws.com/plainviewapp/plainview_1.0.178.zip
Easy way:
set MyApps to {"Google Chrome", "Skype", "Finder"}
repeat with MyApp in MyApps
tell application MyApp
activate
delay 3
tell application "System Events" to tell process MyApp
set value of attribute "AXFullScreen" of window 1 to true
end tell
end tell
end repeat
I'm making an application, in which I need to run a code that will tell system events to keystroke a certain phrase. Like in an AppleScript, I would do:
Tell Application "System Events" to keystroke "This is a test"
I don't know how to do this from Xcode, and I would really appreciate any help. Thank you!
You can run an AppleScript from code, e.g., using OSACompileExecute. Also see QA1026.
Alternately, you can post keyboard events using the CGEvent APIs. With recent OSes, you may be able to avoid posting each letter individually using CGEventKeyboardSetUnicodeString.