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.
Related
How to write the code correctly?
I run the application Photoshop in the automator
I'm waiting for it to fully load
Then I press 10 times Tab and press Enter.
I've tried that:
enter image description here
Looks like that part doesn't work. Because Tab starts to click before the application is fully loaded. What's wrong? Thanks!
repeat until application launch
delay 0.5 end repeat delay 0.5
Most likely, the OP does not understand the main thing: GUI scripting (in this case, sending 10 tabs, and then Enter, that is, keystroke tab and keystroke return in AppleScript language) only works with the frontmost window. And the launch command launches an application without bringing its window to the front.
The correct approach is 1) use the activate application "Photoshop" command 2) use the make new document command, 3) check if the new window exists, 4) send keystroke commands. In the Automator, the Run AppleScript action should be something like this:
on run {input, parameters}
tell application "Photoshop"
activate
make new document with properties {name:"myNewDocument"}
repeat until window "myNewDocument" exists
delay 0.1
end repeat
end tell
tell application "System Events"
repeat 10 times
delay 0.1
keystroke tab
end repeat
keystroke return
end tell
return input
end run
NOTE: not tested, because PhotoShop.app is not installed on my Mac. I am ready to correct my script, if needed. In general, the question is not quite clear.
I don't know much about Photoshop, but I know that it has a loading screen. I tried the following code in Affinity Photo which is a similar product to Photoshop.
tell application "Photoshop"
launch
set theBool to false
repeat until theBool
tell application "System Events" to ¬
if menu item "Close" of ¬
menu 1 of ¬
menu bar item "File" of ¬
menu bar 1 of ¬
application process "Photoshop" exists then ¬
set theBool to true
delay 0.2
end repeat
end tell
The repeat until theBool checks if the loading screen is over by checking if some menu item exists which isn't available when the loading screen is open. If the "Close" and the "File" don't work in Photoshop, you may choose something else.
This is the answer:
tell application "Your app"
launch
activate
end tell
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
What I try to do:
When I'm in one of my text editors (TextEdit, Byword, FoldingText) I want this AppleScript to display the file path.
I figured asking for the frontmost window app get's me the apps name nice and easily and then I can ask for the POSIX path in the next step.
The Problem:
The script is already 99% there, but I'm missing something. When I try to use the variable of activeApp it doesn't work and I get this error:
Error Number:System Events got an error: Can’t get application {"TextEdit"}.
-1728
Here's the script:
tell application "System Events"
set activeApp to name of application processes whose frontmost is true
--This doesn't work either:
--do shell script "php -r 'echo urldecode(\"" & activeApp & "\");'"
tell application activeApp
set myPath to POSIX path of (get file of front document)
end tell
display dialog myPath
end tell
If I exchange activeApp with "TextEdit" everything works. Help would be appreciated.
Maybe there's something in here that helps: Get process name from application name and vice versa, using Applescript
Either get the path property of a document or use System Events to get value of attribute "AXDocument":
try
tell application (path to frontmost application as text)
(path of document 1) as text
end tell
on error
try
tell application "System Events" to tell (process 1 where frontmost is true)
value of attribute "AXDocument" of window 1
end tell
do shell script "x=" & quoted form of result & "
x=${x/#file:\\/\\/}
x=${x/#localhost} # 10.8 and earlier
printf ${x//%/\\\\x}"
end try
end try
The first method didn't work with Preview, TextMate 2, Sublime Text, or iChm, and the second method didn't work with Acorn. The second method requires access for assistive devices to be enabled.
You are asking for...
set activeApp to name of application processes whose frontmost is true
Notice "processes", that's plural meaning you can get several processes in response so applescript gives you a list of names. Even though only one application is returned it's still in list format. Also see that your error contains {"TextEdit"}. The brackets around the name mean it's a list, so the error is showing you the problem.
You can't pass a list of names to the next line of code. As such you have a couple of choices. 1) you can ask for only 1 process instead of all processes. That will return a string instead of a list. Try this code...
set activeApp to name of first application process whose frontmost is true
2) you can work with the list by using "item 1 of the list". Try this code...
set activeApps to name of application processes whose frontmost is true
set activeApp to item 1 of activeApps
Finally, you shouldn't be telling system events to tell the application. Separate those 2 tell blocks of code. Here's how I would write your code.
tell application "System Events"
set activeApp to name of first application process whose frontmost is true
end tell
try
tell application activeApp
set myPath to POSIX path of (get file of front document)
end tell
tell me
activate
display dialog myPath
end tell
on error theError number errorNumber
tell me
activate
display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
end tell
end try
I can't promise the "get file of front document" code will work. That depends on the application. Not all applications will understand that request. That's why I used a try block. In any case though you can be certain you are addressing the proper application. Good luck.
I've been using this snippet for a while, seems to work for all Cocoa apps (not sure about X11):
set front_app to (path to frontmost application as Unicode text)
tell application front_app
-- Your code here
end tell
None of this seems to work with a compiled AppleScript saved as an application and placed on the Dock. Whenever you run the application, IT is the frontmost, not the application that is showing its front window. That application becomes inactive as my Applescript runs. How do I write an Applescript application that isn't active when it runs?
I may have found a solution to the problem listed above. Just tell the user to reactivate the desired application, and give them time.
tell application "Finder"
activate
say "Click front window of your application"
delay 5
set myapp to get name of first application process whose frontmost is true
-- etc.
-- your code
end tell
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