I made a quick translator app in Applescript which takes the whatever you have on the clipboard and puts it into Google Translate. It works with basic A-Z and a few symbols. If the app does not recognize the character, it just writes "a". I need it to support non-Latin charecters, like Japanese and symbols. I tried adding the "as Unicode text" at the end of line 2, but that did not work.
get the clipboard
set translateMe to the clipboard as Unicode text
tell application "Safari"
activate
end tell
tell application "Safari"
activate
open location "https://translate.google.com"
end tell
delay 1
tell application "System Events"
keystroke translateMe
end tell
You could use the URL-Syntax like this:
tell application "Safari"
activate
open location "https://translate.google.com/#auto/en/" & (the clipboard)
end tell
Related
I have several images opened in one Preview window. I'm trying to copy the file name of the selected image in the window.
Currently, I'm using
tell application "Preview" to activate
tell application "System Events" to keystroke "c" using command down
This does not work, probably because it copies the file and its path (I only need the file name as string)
thanks a lot
Maybe this is along the lines of what you are looking for?
tell application "Preview"
set thePath to path of document of front window as POSIX file as alias
end tell
tell application "Finder"
set theName to name of (get properties of thePath)
end tell
set the clipboard to theName
set the text item delimiters to "/"
get the path of the front document in application "Preview"
--> "/Users/CK/Pictures/Screenshots/Screen Shot 2018-03-29 at 16.06.jpg"
get the last text item of the result
--> "Screen Shot 2018-03-29 at 16.06.jpg"
--OR--
get the path of the front document in application "Preview"
tell application "System Events" to get the name of the file result
--> "Screen Shot 2018-03-29 at 16.06.jpg"
--THEN--
set the clipboard to the result
I have a folder of images that gets updated from a camera taking pictures periodically throughout the day. I'm trying to write some applescript that will create a slideshow from a folder of images but also update as more are added without having to rerun the script. I started out trying to do quick look but couldn't get that working. Any ideas on how best to tackle this?
UPDATE
this is what I have hacked together so far:
tell application "Finder" to set the_folder to get folder (choose folder)
tell application "Finder" to open the_folder
tell application "System Events"
tell process "Finder"
key code 124
keystroke "a" using command down
keystroke " " using option down
end tell
end tell
I don't believe this works if I add photos behind the scenes though.
This is what I came up with and it works perfectly for what I need.
tell application "Finder" to set the_folder to get folder (choose folder)
tell application "Finder" to open the_folder
tell application "System Events"
tell process "Finder"
key code 124
keystroke "a" using command down
keystroke " " using option down
end tell
end tell
repeat while true
delay 60
tell application "System Events"
tell process "Finder"
keystroke "a" using command down
end tell
end tell
end repeat
A few caveats... since it is using quick look, you can't really do anything on the computer while this is running since it can't have any other app activate while it is running (quick look closes when this happens). Also the repeat section is required to get quick look to pickup the new additions to the directory without losing focus. Pretty nasty stuff, but I couldn't really find another easy way to do it!
I am still learning the basics of AppleScript.
When I try to run a simple "Hello World" script like:
tell application "TextEdit"
activate
end tell
tell application "System Events"
keystroke "Hello World!"
key code 36
end tell
The first time I run it, it writes "Hello World!" in TextEdit as it should.
But the second time, it writes "Hello World!" in Script Editor.
From then on, it will only write in Script Editor.
Am I missing something obvious in the script?
Or is there something about OS X that I should be looking at?
Thanks in advance! Feel free to respond with long winded answers, Kbase articles, or other discussion threads that I may have missed.
tell application "TextEdit" to activate
tell application "System Events"
tell application process "TextEdit" to set frontmost to true
keystroke "Hello World!" & return
end tell
Try this group of code. I'll explain how it works below, using comments (--).
tell application "TextEdit"
-- Activate TextEdit and bring it to the foreground
activate
-- Create a new document and assign it to a variable called 'myDocument'
set myDocument to make new document
-- Activate the above mentioned new document
tell myDocument
-- Add text to the above mentioned new document
set its text to its text & "Hello, world!"
end tell
end tell
Lately I have discovered Automator (yes, finally she did that) and I wanted to make an event which runs at a regular interval using Calendar. I wanted to save a particular web page to Evernote using the Web Clipper extension in Safari.
I set up my Web Clipper to start on $, F = Full page, enter = Save.
I have come so far as to actually have an event which works by:
Creating a new document in Automator of the type Calendar
Adding "Get Specified URLs" with the URL I want
Adding "Display Webpages"
Adding "Run AppleScript" with the following code - I'm a total noob at AppleScript so you might say I could have done it in a better way ... then do tell ;) ...:
tell application "Safari" to activate
delay 5
tell application "System Events"
keystroke "$" -- key code 10 = Activate Web Clipper (custom shortcut)
end tell
delay 1
tell application "System Events"
keystroke "f" -- key code 3 = Full page saved by Web Clipper
end tell
tell application "System Events"
key code 36 -- works to save page, however, 'keystroke enter' does not
end tell
5.Saving the document in a Calendar event and set it up to repeat.
I found some help here with a list of key code values, however, I couldn't find "enter" in the list. I used a little free app called Key Codes instead to figure out that enter has the key code 36.
I would rather be able to use keystroke, since it is easier to read than some number. Can anyone help?
i'm not sure this will do exactly what you want but maybe just keystroke return ?
tell application "Safari" to activate
delay 5
tell application "System Events"
keystroke "$" -- key code 10 = Activate Web Clipper (custom shortcut)
delay 1
keystroke "f" -- key code 3 = Full page saved by Web Clipper
keystroke return
end tell
I'm experimenting with Applescript for the first time, and am trying to build a script to setup my default layout of applications for developing at work. This involves placing applications across multiple Mission Control spaces. My problem at hand is simply moving about the spaces. I found in many posts similar to this that such action could be achieved with
tell application "System Events"
tell process "Finder"
keystroke "1" using control down
end tell
end tell
if the appropriate key binding was in place. I made the Preferences change so I could use control+1 to move to the first MC space. However, running the script doesn't do anything. This is the event log output:
tell application "System Events"
keystroke "1" using control down
end tell
No errors that I can see, but again: new to Applescript. I've tried many variations of this command including wrapping control down in curly braces and wrapping the call to "System Events" inside a call to "Finder" like this
tell application "Finder"
tell application "System Events"
keystroke "1" using control down
end tell
end tell
but the output is exactly the same with no shift in view. I think I'm missing something here...
According to this question's responses, I tried adding in a delay to make sure I wasn't stepping on my own feet with running the script with CMD-r but nothing happens. I hear the sound effect when you try to click out of an important focus window (if that makes any sense), it's a short beep. Am I talking to the applications improperly?
EDIT
Ok I got something working, but I'm a little confused why this is the case.
tell application "Finder"
activate
delay 0.2
tell application "System Events" to keystroke "a" using control down
end tell
This accomplishes what I need, but I have to change the key binding to a letter. I can replicate the error tone by pressing control+1 when Applescript Editor is active. I guess there's a shortcut for AE that uses the key combo. But why is that running when Finder is supposed to be active?
To answer your question, in your working code the difference is that you activate the Finder before you issue the keystroke command. Keystroke commands are always sent to the frontmost application so you must always make sure to activate an application first as you have done.
If control-1 didn't work then I suspect either some Finder command uses that combo or some other application uses that in a global context meaning it intercepts that command no matter which application is frontmost. Otherwise it should work for you.
Finally, I would remove your system events line of code from the Finder tell block of code. There's no reason the tell the Finder to tell system events to perform a command. Just put that line on its own after the "end tell" line.
Good luck.
I've just been working with this - I don't want to activate finder before triggering what should be a global shortcut. Keystroke was not working, however I tried key code and that indeed works:
on run {}
tell application "System Events"
key code {18} using {command down}
end tell
return "success"
end run
There is a list of key codes at this question: https://apple.stackexchange.com/questions/36943/how-do-i-automate-a-key-press-in-applescript
This should work:
tell application "Finder"
activate
end tell
delay 0.2
tell application "System Events"
key code 18 using {control down}
end tell
I found this tool to be a good alternative that does not have that issue.
Installation is as simple as:
brew install socsieng/tap/sendkeys
Usage:
sendkeys send --initial-delay 0 --delay 0.001 --characters 'Hello'
https://github.com/socsieng/sendkeys