Jump to content

A workflow-action for keystrokes and mouse


dcth

Recommended Posts

It will be great if workflows have a Send keystrokes as an output action, to simulate key pressing.

 

This can facilitate workflows that acts as macros or autocompletion

 

Right now, when I need to return (write, keypress) certain value I must use this osascript

 

tell application "System Events"
    key down return
    keystroke "output value: "
    keystroke "{query}"
end tell
 
But this solution isn't so elegant and tends for lag.
 
I know that modern text editor used to have this kind of features; but a keystroke action provides a generic & direct tool to control the system.
You may agree that sometimes the Automator can't reach your desired option.
 
 
 
Another output action can simulate mouse actions.
 
This tool can simulate different type of mouse actions: clicks, drags, movements, etc
 
 
Currently I'm using this tools to automate repetitive task like
- Replace/filter strings
- Type down values (for keyboard keys)
- Take screenshots of the screen around the cursor
 
 
I hope that some of this stuff make sense for you.
 
Regards
Link to comment
Share on other sites

  • 2 weeks later...

Let me insist with this topic

 

Someone may say that the Copy to clipboard action is what I could use.. since there is a template 'Clipboard > Paste as plain text / text clip from hotkey'

But it's inaccurate. Because even when this action can paste a text on any app, it comes from a copy to clipboard action that replaces your current clipboard value :(

 

What I'm asking is a straightforward action to type the text or keystrokes.

Link to comment
Share on other sites

  • 1 year later...

Add me to this request. One thing I want to use this for is running applications out of VMware Fusion where the same HotKey I use to make Alfred start the application is passed to the application itself to do its work (in this case bring up a sub-window).

 

Passing keystrokes would be handy.

 

Thanks.

Link to comment
Share on other sites

  • 3 months later...

If I want to enter keystrokes via a hotkey, how do you clear the hotkey before it gets to the script?

 

In other words, if I set up a script to type some text using "keystroke", whatever modifier keys I held down to trigger the script affect the output... So, if my hotkey is Cmd + Shift + (whatever), the result doesn't work as the first thing that gets 'typed' by system events is Cmd+Shift+first letter of what you wanted to type, followed by Cmd+Shift+second letter, etc.

How do I stop the hotkey modifiers getting passed through to the script output?

I don't want to do this with snippets, as I don't want to change or use the clipboard.

Link to comment
Share on other sites

  • 2 weeks later...

How do I stop the hotkey modifiers getting passed through to the script output?

I'm afraid the bottom line is: take your fingers off the keys.

 

The whole point of keystroke is to appear exactly like real keypresses to the receiving application. If you've got your finger on the ⌘ key, then that gets sent along with your keystroke command and there's nothing AppleScript can do about that.

If you're using Alfred's Hotkeys, open your Hotkey Settings, right-click on the Hotkey field, and select "Wait until modifier keys are released" from the Trigger behaviour menu. That way, nothing gets run till you let go of the modifiers.

If that's not good enough for you (it's obviously a lot slower), you might want to look for an application that lets you set fn as a modifier: it's far less likely to turn a keystroke into a keyboard shortcut.

Edited by deanishe
Link to comment
Share on other sites

  • 7 years later...
On 3/18/2013 at 7:06 AM, dcth said:

It will be great if workflows have a Send keystrokes as an output action, to simulate key pressing.

 

This can facilitate workflows that acts as macros or autocompletion

 

Right now, when I need to return (write, keypress) certain value I must use this osascript

 

tell application "System Events"
    key down return
    keystroke "output value: "
    keystroke "{query}"
end tell
 
But this solution isn't so elegant and tends for lag.
 
I know that modern text editor used to have this kind of features; but a keystroke action provides a generic & direct tool to control the system.
You may agree that sometimes the Automator can't reach your desired option.
 
 
 
Another output action can simulate mouse actions.
 
This tool can simulate different type of mouse actions: clicks, drags, movements, etc
 
 
Currently I'm using this tools to automate repetitive task like
- Replace/filter strings
- Type down values (for keyboard keys)
- Take screenshots of the screen around the cursor
 
 
I hope that some of this stuff make sense for you.
 
Regards

 

I have following working AppleScript that I want to integrate with Alfred to click at same location for x number of times with delay of y say 2 (is this in sec?)

 

display dialog "Enter number of times to click:" default answer "10"

set repeatmax to text returned of result

 

delay 5

set mousePosition to do shell script "/opt/homebrew/bin/cliclick p:."

set mouseCoordinates to makeCoordinatesRecord(mousePosition)

 

repeat with i from 1 to repeatmax

delay 2

set mousePositionnew to do shell script "/opt/homebrew/bin/cliclick c:" & mousePosition

end repeat

 

on makeCoordinatesRecord(mousePosition)

set a to offset of "," in mousePosition

set x1 to text 1 thru (a - 1) of mousePosition as number

set y1 to text (a + 1) thru -1 of mousePosition as number

set mouseLocation to {x1, y1}

end makeCoordinatesRecord

 

How can I pass x and y as parameters using keywords. I tried following workflow with two blocks keyword connected with RunNSApplescript and Run Script.

 

1. RunNSApplescript with code below

 

on alfred_script(q)
  -- your script here
set repeatmax to value of q delay 5 set mousePosition to do shell script "/opt/homebrew/bin/cliclick p:." set mouseCoordinates to makeCoordinatesRecord(mousePosition) repeat with i from 1 to repeatmax     delay 2     set mousePositionnew to do shell script "/opt/homebrew/bin/cliclick c:" & mousePosition end repeat on makeCoordinatesRecord(mousePosition)     set a to offset of "," in mousePosition     set x1 to text 1 thru (a - 1) of mousePosition as number     set y1 to text (a + 1) thru -1 of mousePosition as number     set mouseLocation to {x1, y1} end makeCoordinatesRecord
end alfred_script

 

Error message:

NSAppleScriptErrorBriefMessage = "Expected \U201cend\U201d but found \U201con\U201d.";
    NSAppleScriptErrorMessage = "Expected \U201cend\U201d but found \U201con\U201d.";
    NSAppleScriptErrorNumber = "-2741";
    NSAppleScriptErrorRange = "NSRange: {355, 2}";

 

 

2. Run Script with code below

 

query=$1

echo -n $query

set repeatmax to value of query delay 5 set mousePosition to do shell script "/opt/homebrew/bin/cliclick p:." set mouseCoordinates to makeCoordinatesRecord(mousePosition) repeat with i from 1 to repeatmax     delay 2     set mousePositionnew to do shell script "/opt/homebrew/bin/cliclick c:" & mousePosition end repeat on makeCoordinatesRecord(mousePosition)     set a to offset of "," in mousePosition     set x1 to text 1 thru (a - 1) of mousePosition as number     set y1 to text (a + 1) thru -1 of mousePosition as number     set mouseLocation to {x1, y1} end makeCoordinatesRecord

 

 

Error message:

no matches found: makeCoordinatesRecord(mousePosition)^M^Mrepeat
/Users/pankajzawar/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/11595308-5499-417A-A3D1-BF09D7D13DDC:5: no matches found: makeCoordinatesRecord(mousePosition)^M

 

Pls guide in fixing. Also explain the difference between Run Script and Run NSApplescript. I am new to this

 

Link to comment
Share on other sites

On 10/5/2022 at 1:09 AM, TomBenz said:

the difference between Run Script and Run NSApplescript.

 

It’s explained at the top of the Run NSAppleScript object. In general, avoid it in favour of Run Script.


Your code does a lot of work to get the mouse position to then use a third-party tool to click. AppleScript is perfectly capable of clicking a position on screen with simpler code and less overhead.


On your second example, where you use query=$1, you’re conflating programming languages. You’re mixing Bash and AppleScript.

 

If you want to pass arguments to the AppleScript, see the placeholder Alfred gives you in the Run Script.

Link to comment
Share on other sites

52 minutes ago, vitor said:

 

It’s explained at the top of the Run NSAppleScript object. In general, avoid it in favour of Run Script.


Your code does a lot of work to get the mouse position to then use a third-party tool to click. AppleScript is perfectly capable of clicking a position on screen with simpler code and less overhead.


On your second example, where you use query=$1, you’re conflating programming languages. You’re mixing Bash and AppleScript.

 

If you want to pass arguments to the AppleScript, see the placeholder Alfred gives you in the Run Script.

 

Thanks. I selected the language and updated the code as below:

 

on run argv
  set theQuery to item 1 of argv
    set repeatmax to theQuery
 
delay 5
set mousePosition to do shell script "/opt/homebrew/bin/cliclick p:."
set mouseCoordinates to makeCoordinatesRecord(mousePosition)
 
repeat with i from 1 to repeatmax
delay 2
set mousePositionnew to do shell script "/opt/homebrew/bin/cliclick c:" & mousePosition
end repeat

display dialog "Done!"
 
end run

on makeCoordinatesRecord(mousePosition)
set a to offset of "," in mousePosition
set x1 to text 1 thru (a - 1) of mousePosition as number
set y1 to text (a + 1) thru -1 of mousePosition as number
set mouseLocation to {x1, y1}
end makeCoordinatesRecord

--  return theQuery

 

It works well. Is there a way to stop running applescripting i.e. clicking if esc is pressed? 

Link to comment
Share on other sites

53 minutes ago, vitor said:

Your code does a lot of work to get the mouse position to then use a third-party tool to click. AppleScript is perfectly capable of clicking a position on screen with simpler code and less overhead.

 

 

I can simulate a mouse click with AppleScript code like this:

tell application "System Events"
  click at {123,456}
end tell

but unable to get mouse position of current using it. I find cursor positions using "Screenshot" tool, activated by Cmd+Shift+4 shortcut generally. Pls advise

Link to comment
Share on other sites

  • 2 weeks later...
On 10/7/2022 at 7:38 AM, TomBenz said:

 

I can simulate a mouse click with AppleScript code like this:

tell application "System Events"
  click at {123,456}
end tell

but unable to get mouse position of current using it. I find cursor positions using "Screenshot" tool, activated by Cmd+Shift+4 shortcut generally. Pls advise

AppleScript is perfectly capable of clicking a position on screen with simpler code and less overhead.

 

@vitor would be great if you can guide on this simpler code. Need to find a way to get mouse position. I am currently getting it via set mousePositionnew to do shell script "/opt/homebrew/bin/cliclick c:" & mousePosition

 

Also how to do you stop a AppleScript that has been run via Alfred and is still running? In script editor, we use cmd + . to stop the code. Something similar to that please

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...