Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Need help with writing a simple key presser script

Hello applescript wizards,

I want to write a script that does this:

press CMD+L,

press /top?sort=top&t=week,

press enter.

My problems:

  1. I know how to make it type individual keystrokes, but have no idea how to make it hold a button (which is necessary to press CMD+L)
  2. Do I have to do

keystroke "A"

keystroke "B"

keystroke "C"

i.e. one by one, or is there a more efficient version?

Here's how it looks like so far:

on run {input, parameters}

tell application "Google Chrome" to activate

tell application "System Events" keystroke ?command? keystroke "L" keystroke "/" keystroke "t" keystroke "o" -and so on, until the end of that text keystroke ?enter?

end tell

return input end run

Big thanks in advance! Your help will be greatly appreciated.

Posted on Jun 2, 2015 8:34 AM

Reply
Question marked as Best reply

Posted on Jun 2, 2015 9:20 AM

keystroke "l" using command down


keystroke "l" using { command down, shift down }


keystroke "/top?sort=top&t=week"

8 replies

Jun 2, 2015 9:38 AM in response to Maciek77M

Ok, I played around a little bit, and here is what seems to work:

activateapplication "Google Chrome"

delay 0.1

tell application "System Events"

keystroke "l" usingcommand down

delay 0.1

keystroke (ASCII character 29)

delay 0.1

keystroke "/top?sort=top&t=week"

delay 0.1

keystrokereturn

end tell


last two questions:

1. do I need an extra "end" at the end of everything, or is it fine as it is?

2. Why did rccharles include "keystroke "l" using { command down, shift down }" ? What does it do, is it necessary?


Big thanks in advance.

Jun 2, 2015 10:36 AM in response to Maciek77M

It implements

(which is necessary to press CMD+L)

keystroke "l" using { command down, shift down }

the lower case L is confusing. People who design fonts like to get l and 1 confused.

hence I would guess

keystroke "l" using { command down, shift down }

and

keystroke "L" using command down

are the same, but I haven't tested this.

Yes, you need the last end. This end the tell. applescript likes balance.

Applescript has some built in constants. tab, return

key code 29

Jun 2, 2015 10:49 AM in response to rccharles

Im still not sure what you mean with the shift down part, but the script works without it, so never mind 🙂


And regarding end: I meant do I need an EXTRA end at the end of everything. it would then look like:


activateapplication "Google Chrome"

delay 0.1

tell application "System Events"

keystroke "l" usingcommand down

delay 0.1

keystroke (ASCII character 29)

delay 0.1

keystroke "/top?sort=top&t=week"

delay 0.1

keystrokereturn

end tell

end

as you can see, there is one more extra 'end' after 'end tell'. is that one more end still needed, or is it redundant?

thank you for your help mate.

Jun 2, 2015 11:38 AM in response to Maciek77M

Im still not sure what you mean with the shift down part,

the key commands simulate key presses on the keyboard. shift down says to hold down the shift key then press the key. So,

keystroke "l" using shift down

give you an upper case L

Applescript compiler should give you an error if wrong. So, no extra end is needed. The default on unit is run. So the more formal syntax would be.


(*

It is easier to diagnose problems with debug information. I suggest adding log
statements to your script to see what is going on.  Here is an example.


    Author: rccharles
   
    For testing, run in the Script Editor.
      1) Click on the Event Log tab to see the output from the log statement
      2) Click on Run

*)


on run
    -- Write a message into the event log.
    log "  --- Starting on " & ((current date) as string) & " --- "
    --  debug lines
    set desktopPath to (path to desktop) as string
    log "desktopPath = " & desktopPath
end run

Jun 2, 2015 12:08 PM in response to Maciek77M

Glad to be of help.



AppleScript

Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X, Third Edition the book


AppleScript Language Guide pdf download the pdf file


Intro to applescript with sending an email ( You are probably beyond this. )
http://mac.appstorm.net/how-to/applescript/the-ultimate-beginners-guide-to-apple script/



>

Need help with writing a simple key presser script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.