Applescript to capture window - applescript

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

Related

Applescript - Keystroke text from variable

I have a file that contains some text. I am able to read this text into a variable. I want to use the keystroke command to send this text to an application. When the line containing the keystroke runs, it produces dialog box that contains an error 'Syntax Error - Can't get keystroke "..content of file.." '
on run {input, parameters}
set inputfile to "/path/to/textfile.txt"
set fileText to read inputfile
keystroke fileText
end run
How can I send the content of a file as keystrokes?
The keystroke command is part of the Process Suite of System Events, therefore when using the keystroke command, it must come from System Events.
Example:
tell application "System Events" to keystroke fileText
However, that said, you need to first set focus to where you want it to be typed, e.g.:
tell application "TextEdit" to activate
delay 1
For example, your code would be:
on run {input, parameters}
set inputfile to "/path/to/textfile.txt"
set fileText to read inputfile
tell application "TextEdit" to activate
delay 1
tell application "System Events" to keystroke fileText
end run

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

How to send dot in AppleScript

I want to upload a file using AppleScript in Chrome. The file path contain a dot (.), but this script is not able to send the dot to 'Go to the folder:'.
on run argv
tell application "System Events"
tell application "Google Chrome" to activate
delay 2
log argv
keystroke "g" using {command down, shift down}
delay 1
keystroke item 1 of argv
end tell
end run
I am running the script with this command:
nsadmins-Mac:util nsadmin$ osascript macUploadChrome.scpt "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py"
This text appears in the "Go to the folder" field after running this script:
/System/Library/Frameworks/Pythonframework/Versions//lib/python/socketpy
That's weird, but what if you try using copy/paste instead:
set the clipboard to (item 1 of argv as string)
keystroke "g" using {command down, shift down}
delay 1
keystroke "v" using {command down}
Why Chrome? Honestly curious, I never have any problems with Safari. Anyways,
--set filepath to POSIX path of theimage
--^^set filepath to the desired file path^^
delay 2
tell application "System Events"
activate application "Safari"
delay 0.5
keystroke "g" using {shift down, command down} --open goto
set the clipboard to filepath
keystroke "v" using {command down}
delay 0.7
keystroke return -- enter goto text
delay 0.4
keystroke return --press enter on file
end tell
Maybe that filepath is just too long. I doubt it though. Try using this, if it doesn't work with Chrome try it with Safari just to see if that's the problem. This is meant to be run when the "file choosing window" is already open, but the full script that I wrote is a bit more extensive, let me know if you want to see it.

Run a command in a new tab in terminal

I'm trying to write a script that could run several commands in several tabs in the terminal.
I found a lot of informations about it, but it doesn't work as I want it to. So I probably need AppleScript.
• This code run a command in a new window:
tell app "Terminal"
do script "echo hello"
end tell
• And this one open a new tab
tell application "Terminal"
activate
tell application "System Events"
keystroke "t" using {command down}
end tell
end tell
But I didn't figured out how to "mix" them. Any idea ?
tell application "Terminal"
activate
tell application "System Events"
keystroke "t" using {command down}
end tell
do script "echo hello" in selected tab of the front window
end tell

Applescript to Expose all Finder Windows?

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}

Resources