Write a character with Applescript - applescript

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.

Related

How to select file using AppleScript in Finder prompt?

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.

How to google Arabic words with Apple Script

I am trying to have Apple Script do some google-searches for me. It works perfectly, as long as the word is in Latin letters. As soon as the word is in Arabic, the code doesn't do anything anymore.
Is there anyone who knows how to solve this?
I have tried using the google-URL for an arabic version of Google but that did not help.
set search to "زق"
open location ("https://www.google.de/#q=" & search)
This code doesn't do anything. If I replace the Arabic letters with any latin letters it works.
It's a puzzle to me, because when I just write "display search" it works as well, so the output of a string with Arabic letters works. If anyone knew of a fix to this, it would be amazing!
What works though is this (you have to first allow System Events to make keystrokes in another application in Settings):
open location ("https://www.google.de/#q=")
delay 6
tell application "System Events" to tell process "Script Editor" to keystroke "زق" & return
delay 4

How do i create a script which presses the spacebar

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.

Is there a way to trigger a Hot Key/Keyboard Shortcut via a Shell Script, a AppleScript or a Automator Workflow?

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

Creating an AppleScript to do a find in a replace in a given document

I'm looking to create aAppleScript that when run will:
Search the document for a given string
Replace that string with another given string
The strings will always be the same
Search for
This will be used in textmate - I was trying to do this in textmate
I know I can use textmate's find and replace functionality - I'm just trying to automate a little.
This should only make changes on the current page.
Is this possible?
UPDATE:
So I've found some code that has got me started...
tell application "TextMate" to activate
tell application "System Events"
keystroke "f" using {command down}
tell process "TextMate"
keystroke "<?"
keystroke tab
keystroke "<?php"
click button "Replace All"
end tell
keystroke "esc"
end tell
but I get the following error:
error "System Events got an error: Can’t get button \"Replace All\" of process \"TextMate\"." number -1728 from button "Replace All" of process "TextMate"
On the find and replace dialog of Textmate the button is labeled "Replace All" Am I missing something here?
You'll have to send the keystroke to the proper window. Something like tell window "find dialog" (or whatever). You have to be completely specific, so it might be
tell tab 1 of pane 1 of window "find and replace" of app textmate...
User interface scripting is so hackalicious you should only do it as a last resort.
Looks like you need sed.
on a command line, or with do shell script:
cat /path/to/your/file.php|sed "s_<?_<?php_g">/path/to/your/newfile.php
or for a whole folder's worth
cd /path/to/your/folder
for file in *.php; do cat "$file"|sed "s_<?_<?php_g">"${file/.php/-new.php}"; done
You're better off looking at MacScripter; there are lots of examples and solutions for find and replacing with or without a texteditor using Applescripts delimiters: MacScripter / Search results, like this:
on replaceText(find, replace, someText)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set someText to text items of someText
set text item delimiters of AppleScript to replace
set someText to "" & someText
set text item delimiters of AppleScript to prevTIDs
return someText
end replaceText
It's all very well sending folks to do search/replace within AppleScript strings, or sending them to Mac OS X's underlying Unix tools, like sed and Perl, but those are often no real substitue for searching/replacing text directly in the target application.
I had the exact same problem, albeit in Dragon Dicate rather than TextMade. Google led me here, where I was dismayed to find no direct solution. So let me share the one I came up with:
set find to "the"
set replace to "THE"
tell application "Dragon Dictate"
activate
tell application "System Events"
tell process "Dragon Dictate"
keystroke "f" using {command down}
keystroke find
keystroke tab
keystroke replace
tell window "Find"
click button "Replace All"
end tell
end tell
end tell
end tell
The key difference is addressing the Find window, which knows about the "Replace All" button. You will also have to change the "Dragon Dicate" target app to "TextMate" of course. (AppleScript seems to require knowing EXACTLY what app a script is being fired against, unless you want to fall back into some truly ugly low-level message sending. When dealing with AppleScript, that's just the 337th sigh of the day!)
If you want to write an AppleScript to manipulate text, there is nothing better than to use an AppleScriptable text editor. That is the right tool for the job. Then you can write just a few lines of code and get the job done.
For example, in TextMate, go Edit > Select All and Edit > Copy to copy the contents of your document to the clipboard. Then run this AppleScript:
tell application "TextWrangler"
activate
set theSearchString to the clipboard
set theResultString to replace "old term" using "new term" searchingString theSearchString
set the clipboard to theResultString
end tell
Then go back into TextMate and go Edit > Paste.
TextWrangler is available for free in Mac App Store.
You may be able to expand this script so that the TextMate work is automated with GUI scripting, but since it is just selecting all and copying and pasting, that is a fairly small task.

Resources