in applescript editor, i would like to know how i can make a script that presses the spacebar every 10 miliseconds, and can be paused and unpaused using a hot key. Sort of like the autohotkey script:
6::pause,toggle
5::
Loop,
{
Send, {Space}
Sleep, 10
}
return
If you're just looking for repeats on the spacebar, why not just put a paperweight on the space bar and let key repeat do the job?
In AppleScript, the repeat part is easy, but the toggle part is not something AppleScript is well-suited for. Instead, you should probably look at something like Keyboard Maestro for this kind of keystroke automation. You can set up Keyboard Maestro macros to alternately start or cancel other macros, which is pretty much what you're asking for.
In Keyboard Maestro you might make one hotkey-triggered macro to run the "repeat until false" applescript below, and another to cancel all macros.
I do not recommend the following.
If you still want to go the AppleScript approach, here's what I might suggest. Don't run this script, since it'll just hit space forever, but you'd set up a script like this:
repeat until false
tell application "System Events" to keystroke space
delay 0.01
end repeat
For testing, you can repeat a specific number of times like so:
repeat with i from 1 to 10
tell application "System Events" to keystroke space
delay 0.01
end repeat
You need to take the first script up there, throw it in Script Editor and "Export" it as an Application. Call it "SMASHYKEY.app". Now, you run that app to mash the space key, but you'll need a way to turn it off.
To turn it off, you'll make a new script:
do shell script "killall applet"
You'll probably want to export that as an App and stick it on your dock to double-click to kill this bastard.
If you want to get fancy with the AppleScript approach, you can throw both of those scripts into separate "Service" workflows in Automator. You can then bind hotkeys to those services from Keyboard Preferences in System Preferences. This is tedious, but I wrote about how to do it, among other methods of running AppleScript.
You're really better off with Keyboard Maestro for this specific kind of task.
Related
I have a massive spreadsheet with a titanic number of rows/columns (e.g. ~250 columns, many thousands of rows) that I'm trying to convert into PDFs by looping through each row with AppleScript, copying that row's ~250 variables to TextEdit set to Rich Text (for bold formatting etc), and then using System Events to save the txt as a PDF. Here's a summary of the code:
on run
set initialRow to 1
tell application "System Events"
tell application process "TextEdit"
set frontmost to true
end tell
end tell
repeat
tell application "Microsoft Excel"
-- CLEAR MY ~250 VARIABLES FROM PREVIOUS ROW'S VALUES TO MAKE SURE NOTHING IS CARRIED OVER BY MISTAKE
-- THEN SET MY ~250 VARIABLES TO THE NEXT ROW'S VALUES
if exampleValue is "" then exit repeat
end tell
tell application "TextEdit"
set the text of the front document to ""
-- THEN SET FIRST PARAGRAPH TO MY FIRST VARIABLE PLUS A LINE BREAK SO THEN THERE'S A NEW PARAGRAPH FOR THE NEXT VARIABLE, ETC
-- THEN GO THROUGH ALL OF MY VARIABLES TO IMPORT THE IMPORTANT ONES INTO TEXTEDIT, SET SOME FORMATTING, ETC.
end tell
delay 1
tell application "System Events"
click menu item "Export as PDF…" of menu 1 of menu bar item "File" of menu bar 1 of application process "TextEdit"
delay 1
keystroke exampleValue -- SYSTEM EVENTS TYPES THE NAME OF THE PDF
delay 1
key code 36
delay 1
end tell
set myRow to (myRow + 1)
end repeat
end run
This all runs great, no bugs (seemingly!), no issues at all in small doses. The problem, however, is that something happens as the script runs where it seems to be tying up more and more memory somewhere; everything is fine for the first hundred or so rows, but at some point my Mac stops running anything at all, i.e. whether I let the script run until it starts producing super random errors (I could collect them if helpful, but it's like a random different error each time so not much help there) or even if I let the script run for a while and then stop it before it errors out - it will let me stop the script but then I can't actually quit out of Script Editor or TextEdit or Excel, my keyboard stops working, I can't Force Quit anything, can't Reset the computer, etc. It's just a complete meltdown of the machine unlike anything I've encountered, and the only way to get back to work is to force a hard boot with the power button.
I've never had this problem with my other scripts, but I also don't usually use System Events, so my hunch is that it's something to do with that. Do I need to be 'resetting' System Events somehow, or clearing out the memory for some reason, or...? Thanks for the help!!
Figured it out! After trying the script one more time with Activity Monitor running, I discovered that each time it iterates through, 3 new processes were popping up - Core Sync, Dropbox Finder Extension, and SnailSVNLite - and then never going away! So if I ran through the script 500 times, I'd end up with 1500 new processes running, which was almost certainly what was wrecking me though I have no idea why telling System Events anything was doing that. I looked around online, and it turns out those are all Finder Extensions that had been turned on at some point long ago, so just needed to go to System Preferences > Extensions > Added Extensions and then uncheck those 3 extensions - and then problem solved!!
I am working with Selenium on macOS to automate sending images using WhatsApp web in Google Chrome. The task involves uploading the image, and for that a system(Finder) prompt comes up to select the file. It's done in Windows using AutoIt.
I tried looking up how to automate this task in macOS, and I believe AppleScript can be used for it. Since I have no experience in GUI scripting, any help would be appreciated.
Thanks.
I was able to find the answer on another post on Stack Overflow. I have added the answer for anyone who comes across the same problem.
tell application "System Events"
keystroke "G" using {command down, shift down}
delay 1
keystroke "/path/to/file"
delay 1
keystroke return
delay 1
keystroke return
delay 1
end tell
I don't advocate GUI scripting any more than the burning down of the Amazon, but it seems to be necessary for this task, and I wanted to provide you with an example of a GUI script that tries its best to minimise the unpleasantness of the user experience, and aim for fewer weak points in the code where GUI scripts are most likely to falter.
If you know the path to your file—which I assume you do in these sorts of situations, as your script keystrokes the filepath—then you might find the following technique saves a few steps, and feels a bit more graceful in how it gets executed:
set filepath to "/path/to/image.jpg"
-- Copy file object to clipboard
set the clipboard to filepath as «class furl»
-- Make sure Chrome is in focus and the
-- active tab is a WhatsApp tab
tell application id "com.google.Chrome"
activate
if the URL of the active tab in the front window ¬
does not contain "web.whatsapp.com" then return
end tell
-- Paste the clipboard contents
-- and hit return (send)
tell application id "com.apple.SystemEvents"
tell (process 1 where it is frontmost) to tell ¬
menu bar 1 to tell menu bar item "Edit" to tell ¬
menu 1 to tell menu item "Paste" to set Paste to it
if (click Paste) = Paste then keystroke return
end tell
The if (click Paste) = Paste check should negate the need for a delay, as it explicitly forces AppleScript to evaluate the click command before going on to issue a keystroke. However, I can't test this under all possible conditions, and if there are other factors, like CPU usage, or process freezes, that are likely to give the script a chance to jump ahead, then just insert a small delay after then and move keystroke return down onto its own line.
If you wish to remove the file object from the clipboard afterwards, then simply add as the final line set the clipboard to (and just leave it blank after the word "to", which will clear the clipboard's contents). Of course, this won't affect any clipboard history data you might have if you use a clipboard managing app, only the system clipboard's current item.
I know you can perform keyboard shortcuts with AppleScript like this:
-- to perform Cmd + P
tell application "System Events"
keystroke "P" using {command down}
end tell
Now I would like to take it a step further. I would like to perform a mouse click while a modifier is down.
I tried the following:
-- to perform Cmd + click
tell application "System Events"
key down command
tell application process "Some application" to tell button "SomeButton" to click
key up command
end tell
But this doesn't yield the same result as physically performing the Cmd + click.
How can I achieve my goal?
Indeed it may be better to script this directly, if possible. However, if it is not possible you can always click on any point on the screen while holding down a modifier key. For example:
do shell script "cliclick kd:cmd c:28,11"
will click on screen coordinates (28, 11) while holding down the command key. The screen coordinates you want to click can be manually identified with your cursor by holding down command + shift + 4. To make the above script work you will need a copy of the free Terminal/Command Line app cliclick.
It would be easier to try to think about what event you are trying to trigger instead of using the mouse to do it. Afraid I can't be of more help, but that would be my approach.
I'm an avid Keyboard Maestro user and I need a workaround for triggering a keyboard shortcut like ⌘⇧L (externally, without Keyboard Maestro). So I thought a bash script would be capable of doing such a thing. An AppleScript or an Automator workflow would be sufficient, too. I anybody could help me this would be great.
You don't have to read this, but here's why I want to do what I want to do:
I have a the same string assigned to various Markdown macros, I use a string instead of Hotkeys because it's much more memorable for me since my brain already is filled with so many application shortcuts. The disadvantage is that Keyboard Maestro won't delete the keystrokes of the string. I can perform several actions within the program to delete them but adding these actions for each and every macro is tedious and suboptimal.
tell application "System Events" to keystroke "l" using command down & shift down
tell application "System Events"
key code {123, 124} using {shift down, command down} -- ⇧⌘←, ⇧⌘→
keystroke "c" using command down -- keystroke "C" would be treated as ⇧C
end tell
delay 0.02 -- you need a small delay here before the next command
set txt to Unicode text of (the clipboard as record)
Reference of Mac key codes: lri.me/chars
Really basic, but how to write a character with Applescript. My 'd' key is broken, i'd like to assign a script to a f1-12 key just to write that character in any application. Thank's!
This should do: tell application "System Events" to keystroke "d"
It would be easier if you simply acquired a new keyboard.
Not all applications are scriptable, GUI scripting is unreliable, and sometimes even the simplest of scripts can take multiple seconds to run, so Applescript really isn't a reasonable solution to your broken keyboard problem.