Applescript to Expose all Finder Windows? - macos

I'm trying to figure out how to write an Applescript that will Exposé all Finder Windows.
Application Logic
I think the script needs to have this application logic:
Check a residual setting and get the name of the last "Frontmost
Application" (perhaps use a text file ~/last-application.txt to store this?)
Grab the name of the current Frontmost Application
If the name of the current of the Frontmost Application is Expose, then activate the previous frontmost application
Else, activate finder, and then activate expose for just finder windows
Desired Behavior
When the script is activated all the finder windows (and only the finder windows) will be shown in Exposé
If the script is then run again (and no finder window was selected) the script will just switch back to the last frontmost application
I'm not sure how to get this working though. If there is another utility that does this automatically that'd be great, too.

set f to "/s/x/finderexpose"
set prev to do shell script "touch " & f & "; cat " & f
if prev is not "" then
delay 0.5 -- time to release modifier keys used in a shortcut
tell application "System Events" to key code 53 -- esc, if Exposé is open
delay 0.3 -- for the Exposé animation?
activate application prev
do shell script "echo '' > " & f
else
do shell script "echo " & quoted form of (path to frontmost application as text) & " > " & f
activate application "Finder"
delay 0.05
tell application "System Events" to key code 125 using {control down} -- ⌃↓
end if
It'd be less ugly if the part for switching to the previous application was left out:
activate application "Finder"
delay 0.05
tell application "System Events" to key code 125 using {control down}

Related

AppleScript shortcut for service not working, although clicking on application generated by Script Editor works fine

I created an AppleScript for a sequence of copy-paste keystrokes and delays:
Example AppleScript code:
tell application "System Events"
keystroke "k" using command down
delay 0.1
keystroke "a" using command down
delay 0.1
keystroke "c" using command down
delay 0.1
tell application "Google Chrome"
if it is running then
quit
else
activate
open location "http://translate.google.com"
delay 1
activate
delay 0.7
end if
end tell
tell application "System Events"
keystroke "v" using command down
delay 0.7
keystroke "c" using control down
end tell
end tell
I've exported it from Scripts Editor as an Application, and it works fine when I click on it.
When I try to execute it via a shortcut set at Systems Preferences > Keyboard > Shorcuts > Services, nothing happens; I just see the cog-wheel appearing briefly at the top bar. I already granted permission for the script-app at Systems Preferences > Security & Privacy > Accessibility and already checked if there are hotkeys conflicts at the Terminal typing: defaults find NSServicesStatus or defaults find '#~$]' (the shortcut I've tried to use was Command+Alt+Shift+].
Would you maybe have any suggestion of where can I check what I'm doing wrong?
Here is an option which takes a completely different route, without having to resort to using UI scripting and without having to open any browsers.
To be able to use my solution, you'll have to install the translate-shell shell command. In Terminal.app, I installed it using Homebrew with this command… brew install translate-shell
After successful installation of the translate-shell shell command, it can then be used in AppleScripts and Automator Workflows
This following AppleScript code will take the text, which is currently on your clipboard, and translate it to the language of your choice. It will then set the content of your clipboard to the translated text.
I took the liberty to add a few Language Codes to get you started.
property convertLanguage : {"Convert To Belarusian (be)", "Convert To Bulgarian (bg)", ¬
"Convert To Dutch (nl)", "Convert To English (en)", "Convert To Estonian (et)", ¬
"Convert To French (fr)", "Convert To German (de)", "Convert To Greek (el)", ¬
"Convert To Hebrew (he)", "Convert To Hungarian (hu)", "Convert To Italian (it)", ¬
"Convert To Polish (pl)", "Convert To Romanian (ro)", "Convert To Russian (ru)", ¬
"Convert To Spanish (es)", "Convert To Swedish (sv)", "Convert To Ukrainian (uk)"}
activate
set chosenLanguage to word 4 of ((choose from list convertLanguage ¬
with title "Language Translator" with prompt ¬
"Choose A Language To Convert To" OK button name ¬
"Translate" cancel button name "Cancel") as text)
convertToLanguage(chosenLanguage)
on convertToLanguage(twoLetterLanguageCode)
set textToConvert to the clipboard
delay 0.1
set the clipboard to ¬
(do shell script "export PATH=\"/usr/local/bin:$PATH\";/usr/local/bin/trans -b :" & ¬
quoted form of twoLetterLanguageCode & " " & ¬
quoted form of (textToConvert as text))
end convertToLanguage
This following animation demonstrates the conversion of the English text (already on my clipboard) to French. Then I paste the converted text into the document.
You should reduce GUI scripting to have more stable code. Workaround is shown here:
my copyTextToClipboard()
set sourceText to (the clipboard) as string
my performGoogleTranslate(sourceText)
my getTranslatedTextToClipboard()
on copyTextToClipboard()
tell application "System Events"
keystroke "k" using command down
delay 0.1
keystroke "a" using command down
delay 0.1
keystroke "c" using command down
delay 0.1
end tell
end copyTextToClipboard
on performGoogleTranslate(sourceText)
-- following translates from English (en) to Russian (ru)
-- you can put other languadges settings
set myURL to "https://translate.google.gr/?
hl=el#view=home&op=translate&sl=en&tl=ru&text=" & sourceText
tell application "Google Chrome"
activate
set URL of active tab of window 1 to myURL
end tell
end performGoogleTranslate
on getTranslatedTextToClipboard()
-- HERE you need some mouse tool to move the mouse pointer over
-- the "Copy" button of Google Translate and to click it
end getTranslatedTextToClipboard

Run emacs using applescript and open file

What I would like to do is have an automator script open a file in emacs when I drag a file onto it, and bring terminal to the front.
Right now, when I try to do this with input set to ~/Desktop/test.txt, it either opens up the main page, or it ends up with ~/Desktop/testtxt. Am I doing this wrong? Is there a better way to do this?
This is what I have right now:
set thePath to POSIX path of input
tell application "Terminal" to do script "emacs"
tell application "System Events"
tell process "Terminal"
activate
delay 2
keystroke "x" using control down
delay 1
keystroke "f" using control down
delay 1
keystroke thePath
delay 1
keystroke return
delay 1
end tell
end tell
return input
end run
Use the file path as an argument to emacs
on run {input}
tell application "Terminal"
repeat with i in input
do script "emacs " & quoted form of POSIX path of i
end repeat
activate
end tell
return input
end run

Execute shortcut or menu action in particular application window

Is there a script option to actualy go through all XCode opened projects (application windows) and execute menu action Product->Stop or executing shortcut "CMD + ."?
To run Apple Script in Xcode at the beginning of each run You can create shell script like so:
#!/bin/bash
osascript -e 'tell application "Xcode"
activate
set activeWindow to window 1
set windowName to name of activeWindow
repeat with aWindow in windows
tell application "System Events" to keystroke "`" using command down
if name of aWindow is not equal to windowName then
if name of aWindow is not equal to "" then
tell application "System Events" to keystroke "." using command down
end if
end if
end repeat
end tell'
Then in Xcode go to Preferences->Behaviours->Starts->Run and choose Your script.

Applescript to capture window

I am trying to create an applescript to capture a window. The keyboard shortcut is cmd+shift+4 then space. I am not able to use this in applescript.
My code :
tell application "system events"
keystroke "21, 49" using {command down, shift down}
end tell
It doesnt work. The problem with the scrip it using space bar. I need to hold cmd, shift & 4 and then press space bar.
Try:
tell application "System Events"
keystroke (ASCII character 36) using {command down}
delay 1
keystroke space
end tell
This Applescript might work better for you rather than using GUI scripting
it uses the screen capture command line. for more info look at the screencapture Man page
set fileName to do shell script "date \"+Screen Shot %Y-%m-%d at %H.%M.%S.png\""
tell application "System Events" to set thePath to POSIX path of desktop folder
do shell script "screencapture -W " & "\"" & thePath & "/" & fileName & "\""
If all you want is to get to that point then use this:
tell application "System Events"
key code 21 using {shift down, command down}
delay 0.1
key code 49
end tell

applescript: how to automatically choose to save a safari document at a PARTICULAR PATH with system events

I want to make a script that will save safari documents using system events (command+s) and I want the file to always be saved at a particular path. (I could make a habit of always saving safari files at a certain folder but that's not a robust solution) How can I make sure system events saves a document at a certain path?
Directly from this MacRumors forum post:
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari"
click menu item "Copy" of menu "Edit" of menu bar 1
end tell
end tell
delay 1
set the_filename to (the clipboard) & ".html"
set the_filepath to "Macintosh HD:Users:Roy:Documents:" & the_filename
set the_shellfilepath to "'/Users/Roy/Documents/" & the_filename & ".download'"
set the_shellfilepath2 to "'/Users/Roy/Documents/" & the_filename & "'"
tell application "Safari"
activate
save document 1 in the_filepath
end tell
do shell script "mv " & the_shellfilepath & " " & the_shellfilepath2
Set the path (the_shellfilepath) as appropriate.
Changing the folder of a file dialog:
try
try -- this would result in an error when there's no clipboard
set old to the clipboard
end try
-- delay 1 -- for testing
-- activate application "Safari"
tell application "System Events"
keystroke "s" using command down
keystroke "g" using {shift down, command down}
delay 0.1
set the clipboard to "~/Movies/"
delay 0.1
keystroke "av" using command down
delay 0.1
keystroke return
delay 0.1
end tell
set the clipboard to old
end try
Saving to a specific folder with a partially encoded URL as a file name:
tell application "Safari"
set x to URL of document 1
set r to do shell script "echo " & quoted form of x & " | sed 's|/$||;s|:|%3A|g;s|/|%2F|g'"
do shell script "curl " & x & " > " & quoted form of ((system attribute "HOME") & "/Desktop/" & r & ".html")
end tell
Saving to a specific folder but choosing the file name manually:
tell application "Safari"
set x to URL of document 1
set answer to text returned of (display dialog "" default answer ".html")
do shell script "curl " & x & " > " & quoted form of ((system attribute "HOME") & "/Desktop/" & answer)
end tell

Resources