I'm trying to write a simple applescript script to get the contents of WriteRoom (a simple text editor) and run it through a markdown parser then copy the resulting html to the clipboard:
tell application "WriteRoom" to activate
tell application "System Events" to keystroke "a" using command down
tell application "System Events" to keystroke "c" using command down
set the clipboard to (do shell script "cd ~;echo \"" & (the clipboard) & "\" >> writeroom.md; /usr/local/bin/markdown writeroom.md")
but when I run it I get an error sometimes:
Can’t make {«class RTF »:«data RTF 7B5C727466315C616E73695C616E7369637067313235325C636F636F61727466313033385C636F636F617375627274663335300A7B5C666F6E7474626C5C66305C6673776973735C6663686172736574302048656C7665746963613B7D0A7B5C636F6C6F7274626C3B5C7265643235355C677265656E3235355C626C75653235353B7D0A5C706172645C74783536305C7478313132305C7478313638305C7478323234305C7478323830305C7478333336305C7478333932305C7478343438305C7478353034305C7478353630305C7478363136305C7478363732305C716C5C716E61747572616C5C7061726469726E61747572616C0A0A5C66305C66733332205C636630202A20746573745C0A2A20617364665C0A2A206E6F5C0A2A207465737474657374746573747D», «class utf8»:"* test
* asdf
* no
* testtesttest", «class ut16»:"* test
* asdf
* no
* testtesttest", uniform styles:«data ustl0200000090000000000000001400000020000000010000002100000000000000010000006C000000040000000000000000000000020100000100000000000000050100002C000000646D616E2400000001000000040000000100000000000000000000000900000048656C76657469636100000006010000040000000000100007010000060000000000000000000000»} into type Unicode text.
The selected text doesn't seem to be copied to the clipboard, instead whatever I had on my clipboard is converted. Any ideas?
It seems that text from your application is copied as a RTF stream. To convert it to a simple text please try using as text:
(the clipboard as text)
Update
After reading your question again, I I've downloaded WriteRoom and came up with this solution:
tell application "WriteRoom" to activate
tell application "System Events"
tell application process "WriteRoom"
set content to (value of text area 1 of scroll area 1 of front window) as text
end tell
end tell
display dialog content
Then you can use the content variable for further processing. Use the Mac program Accessibility Inspector to find out the UI definition of any window as I did.
It's an old question and I see you got the fix.
But seeing that I get the same "An error of type -25130 has occurred. Number -25130" with an Applescript (not using WriteRoom, using Sigil) I find out another fix in these cases.
Often it's really very important to add a small delay using GUI scripting. And actually even with some pre-selected text and just having a single keystroke I got that error. Not so if delay 0.1 before and after keystroke (or you have to play with different delay 0.2 and so on).
delay 0.1
tell application "System Events" to keystroke "c" using command down
delay 0.1
Related
I am trying to automate a repeating task between Numbers and Quicken 2017 using AppleScript.
I would like to take the contents of the currently selected cell in Numbers, set the clipboard with that numeric value, and paste that value into the search field in Quicken.
How can one go about doing that with AppleScript?
Example pseudo code to illustrate intent:
tell application "Numbers"
activate
set myCellsValue to value of currently selected cell
set the clipboard to myCellsValue
end tell
tell application "Quicken 2017"
activate
tell application "System Events"
keystroke "f" using {command down}
delay 0.1
keystroke "v" using {command down}
end tell
end tell
You got it 90% correct. I don't use Quicken, but this worked with Microsoft Word:
tell application "Numbers"
activate
tell active sheet to tell application "System Events" to keystroke "c" using command down
end tell
tell application "Quicken 2017"
activate
tell application "System Events"
keystroke "f" using command down
delay 0.1
keystroke "v" using command down
end tell
end tell
I generally like to minimise the amount by which I employ System Events to issue keystrokes or mouse clicks if there’s another way.
Numbers is very much AppleScript-able. I don’t use it personally, but with the help of this site, I’ve pieced together this example script that I wholly admit is untested (I’d appreciate your feedback if you try it out):
tell application "Numbers"
tell the front document to ¬
tell the active sheet to ¬
set CurrentTable to the first table whose class of selection range is range
tell the CurrentTable to get the value of the first cell in the selection range
if the result is not missing value then set the clipboard to the result
end tell
Quicken is also AppleScript-able, but I can’t find a downloadable copy of Quicken’s AppleScript dictionary in order to piece together an equivalent sample of code. However, your Quicken tell block is exactly right for employing System Events to issue a Cmd+V.
However, if you fancy uploading a PDF of the AppleScript dictionary, I can use it to try and draft something more robust.
I use firefox for a long time as my only browser on Pc or Mac.
In a few words my problem: I want to create a service on mac with automator and
Applescript for quite instant translation using translate.google.com.
What works great with Safari or Chrome (below the 4 or 5 lines of script)
On run {input, parameters}
Tell application "Safari"
activate
try
Open location "https://translate.google.com/#auto/en/" & (the clipboard)
end try
end tell
end run
The same thing (script) does not work at all with Firefox, I try by different ways
To circumvent the impossible problem
On run {input, parameters}
Set theProcess to "Firefox"
Set info to {}
Set y to 0
Set x to 0
Set n to 0
Tell application "Applications / Firefox.app"
activate
Open location "http://translate.google.com/#auto/en/"
end tell
Tell application "System events"
Repeat with theProcess in (process "Firefox")
try
Set info to info & (value of (first attribute whose name is "AXWindows") of theProcess)
end try
end repeats
Set n to count of info
info
end tell
Tell application "System Events" to tell process "Firefox"
Set x to "1"
Set frontmost to true
try
Click menu item "Paste" of menu "Edit" of menu bar 1
end try
Repeat while x is "1" -
If x is "1" then
Keystroke "V" using command down
Set x to "0"
end if
end repeat
end tell
end run
By copying and pasting, the actions take place before the complete loading of the page, even by slowing down the Copy and Paste procedure.
After many observations there is a problem of formatting the text contained in the clipboard with the association of the URL, I improved this but it is not perfect yet.
tell application "Applications/Firefox.app" to activate
tell application "System Events" to tell process "Firefox"
set frontmost to true
set sentence to text of (the clipboard)
set thesentences to paragraphs of sentence
set thenewsentences to thesentences as string
set the clipboard to thenewsentences
keystroke "t" using command down
keystroke "https://translate.google.com/#auto/fr/" & (the clipboard) & return
end tell
Anyway if it works with Safari without modifying anything, the problem is at the Firefox entry, so if you can look at this problem, that would be very useful to us all.
Thank you for your attention.
Thank you for your answers.
Safari and Chrome perform the necessary encoding of reserved characters in the URL for you, but Firefox doesn't.
Therefore, you need to perform the encoding of the query-string value (the text to embed in the URL) explicitly.
The easiest (though not obvious) approach is to use perl, via a shell command, gratefully adapted from here:
# Example input that contains chars. that have special meaning in a URL ('&' and '?')
set the clipboard to "Non, Je ne regrette rien & rien ne va plus?"
# Get text from the clipboard and URL-encode it.
set encodedText to do shell script ¬
"perl -MURI::Escape -lne 'print uri_escape($_)' <<<" & quoted form of (the clipboard)
# Now it's safe to append the encoded text to the URL template.
tell application "Firefox"
activate
open location "https://translate.google.com/#auto/en/" & encodedText
end tell
The above approach works with all three browsers mentioned: Firefox, Safari, and Google Chrome.
Note:
As of (at least) Firefox v50, Firefox opens the URL in in a new tab in the current front window by default.
You can make Firefox open the URL in a new window instead, by unchecking Open new windows in a new tab instead on the General tab of Firefox's preferences.
Note, however, that this is a persistent setting that affects all URLs opened from outside of Firefox.
For an ad-hoc solution for opening in a new window that doesn't rely on changing the setting, see this answer of mine.
Hello below the service Automator with some version it is possible to encounter problems, I modified the script so that it works almost everywhere.
A frequent system error is the permission to applications to control your computer that is handled by the system preferences tab Security and Privacy, the system asks if you allow Firefox, TexEdit and others using this service for its keyboard shortcuts.
Also in Automator create service (to be General (and appear in all applications) no entry (up to Yosemite since El Capitan I saw that with Firefox for example all the services are usable), choose Execute a script Applescript paste the script below divided into 2 script or 1 script only.
on run
set Sn to ""
tell application "System Events"
set Sn to the short name of first process whose frontmost is true --here we look for and find which application to launch the service
tell process Sn
set frontmost to true
try
click menu item "Copy" of menu "Edit" of menu bar 1 -- there is used the Copier of the menu Editing of the application
end try
end tell
end tell
return the clipboard
end run
In the script following the entry is done with the contents of the Clipboard
On run {input}
on run {input}
set input to (the clipboard) -- Here we paste the contents of the Clipboard into a variable
try
set input to do shell script "Perl -MURI :: Escape -lne 'print uri_escape ($ _)' <<< " & quoted form of input --To format the text to make it usable with Firefox and the url of translate.google.com
tell application id "org.mozilla.firefox"
activate
open location "https://translate.google.com/#auto/en/" & input --here since version 50 of Firefox you must open a tab and not a new url window of translate.google.com, with option #auto automatic detection by google of the language, and fr to translate into French (these options are modifiable at will)
end tell
end try
end run -- end of the operation you have a tab open on translate, a text to translate and a translated text
I have many untitled TextEdit files. I'd like to use applescript to save each using, as a name, the text of the top line of each document.
The following will select and copy the first line of a document (not elegant, but it works), but I can't figure out how to paste the clipboard into the save dialog box (and hit "save" afterwards). Can anyone help?
tell application "TextEdit" to activate
tell application "TextEdit"
tell application "System Events" to key code 126 using command down
tell application "System Events" to key code 125 using shift down
tell application "System Events" to key code 8 using command down
end tell
There are 2 ways of doing:
1) the method using GUI scripting: this is what you've started to do. You simulate keyboard events like a user. It is not recommended for mainly 3 reasons: It is usually slow (you need to add delays to leave time for system open window, close them,..). During the script, if user hits key/mouse by mistake, your script will fail. And finally, you're hardly dependent of user interface of the application: if the editor (here Apple with TextEdit) changes something, like a short cut key, your script will no longer work.
Despite that, if you still want to use that way, here is the script that does it for you. I recommend that you add comments as I did (how to remember that key code 8 is 'c' !). I added some extra options to select the path to save (go home folder, enter special path,...). Up to you to use them or not:
tell application "TextEdit"
activate
tell application "System Events"
key code 126 using command down -- command up (cursor at start)
key code 125 using shift down -- shift down (select 1st line)
keystroke "c" using command down -- command C (copy)
keystroke "s" using command down -- open save dialog
delay 0.5 -- to let save as dialog time to open
keystroke "v" using command down -- paste the title from clipboard
-- other options
-- keystroke "h" using {command down, shift down} -- go home directory
delay 0.5
keystroke "g" using {command down, shift down} -- go to dialog
delay 0.5
keystroke "Desktop/Sample" -- path from Documents folder to Sample folder on Desktop
delay 0.5
keystroke return -- close the go to dialog
delay 0.5
keystroke return -- close the save as dialog
end tell
end tell
2) the method using Applescript instructions. It is usually much shorter, more elegant script, much faster to run, and user can't break it during execution. The script bellow does same as script above: It selects the first text row and save the document with that title. Line 1 defines the folder where to save:
set myPath to (path to desktop folder) as string -- path where to save file
tell application "TextEdit"
activate
tell front document
set myTitle to first paragraph
set myTitle to text 1 thru -2 of myTitle -- to remove the return at end of paragraph
save in (myPath & myTitle)
end tell
end tell
I hope it helps
I'm using a third party application to copy an image to the clipboard. I would then like to perform an AppleScript that opens Preview, creates a New Document which will then use the clipboard as the content, then save this to a specified location.
I'm 99% of the way there - just can't get the document to save. I'm on OS X 10.9.5.
Here's my script so far:
set the save_location to the quoted form of "/Users/freddy/Documents/test.png"
tell application "Preview"
activate
end tell
tell application "System Events"
tell process "Preview"
keystroke "n" using {command down}
end tell
end tell
tell application "Preview"
activate
save front document as "PNG" in POSIX file save_location
end tell
I can't find the correct syntax for saving a Preview document. It will be the only document open at the time.
Try the following - note the comments:
# Do not use `quoted form of` - only needed and useful with `do shell script`
set the save_location to "/Users/jdoe/Documents/test.png"
tell application "Preview"
activate
end tell
tell application "System Events"
tell process "Preview"
keystroke "n" using {command down}
end tell
end tell
tell application "Preview"
# Wait until the new window appears.
# Trying to save before the window has appeared will fail.
# Note: - This assumes that NO window was initially open.
# - The code should be made more robust to eventually time out.
repeat until (count of windows) > 0
delay 0.3
end repeat
activate
# Save the document; `as "<format>"` doesn't seem to work, but the
# format is *implied* by the filename suffix (extension).
save front document in POSIX file save_location
end tell
This script works to save an image in the clipboard to disk, using Preview in OS 10.11.6 on a 2010 Mac Mini:
--CONDITIONS: Finder's clipboard has an image in it
tell application "Preview"
launch
activate
end tell
delay 2 --tweak the delays to what works
tell application "System Events" --not as elegant as "tell application...to tell process", but clearer, chronologically
keystroke "n" using command down --Preview creates a new window.
delay 1
keystroke "v" using command down --Finder's clipboard image is pasted into Preview window.
end tell
delay 1
tell application "System Events"
set the clipboard to "Joe Blow" --for name of forthcoming Preview file
delay 1
keystroke "w" using command down --Forces prompt of Preview to save image.
delay 1
keystroke "v" using command down --Pastes new filename into Preview's save window.
delay 1
key code 36 --"Return" key saves the new file.
end tell
--RESULT: A new image exists in your default folder, named "Joe Blow.png" (or whatever extension)
I am trying to setup a TextWrangler script that automatically sends selected code to Julia. Copying a bit from a similar script that does the job for sending code to R I tried the script
tell application "TextWrangler"
set the_selection to (selection of front window as string)
if (the_selection) is "" then
set the_selection to line (get startLine of selection) of front window as string
end if
end tell
tell application "Julia-0.2.1"
activate
cmd the_selection
end tell
It does not work. Probably because of the line containing "cmd the_selection". Anyone a suggestion on how to fix this?
Well if Julia isn't scriptable as #khagler says, then you can usually use gui scripting. This will work as long as Julia has an edit menu and the keyboard shortcut for paste is cmd-v (like in most applications) and your cursor is placed in the proper location for the paste to work. Good luck.
tell application "TextWrangler"
set the_selection to (selection of front window as string)
if (the_selection) is "" then
set the_selection to line (get startLine of selection) of front window as string
end if
end tell
set the clipboard to the_selection
tell application "Julia-0.2.1" to activate
delay 0.2
tell application "System Events" to keystroke "v" using command down
EDIT: I see in your comments that a new Julia window opens every time the script is run. That might be happening because of the line... tell application "Julia" to activate. You might try this line of code in its place. It's another method to bring Julia frontmost before issuing the keystroke commands.
tell application "System Events" to tell process "Julia-0.2.1" to set frontmost to true
Note that if this doesn't bring the Julia window frontmost then it's because the process name is wrong. Sometimes the process name is different than the application name. If true you'll have to figure out the name of the Julia process which you can do by running this and finding its name. You may have to use "Terminal" if Julia runs in a Terminal window.
tell application "System Events" to return name of processes
Julia is not scriptable, so you won't be able to send your code this way. You could try this:
do shell script "julia -e '" & the_selection & "'"
This is equivalent to typing a command into a Terminal window. Depending on what exactly is in your selection, you might need to save it as a file first and then pass the file path instead. Also, note that if julia is not in your path (as would be the case if you just copied the Julia app to your Applications folder) you'll need to specify the full path to the actual executable within the .app package: /Applications/Julia-0.2.1.app/Contents/Resources/julia/bin/julia.