Run emacs using applescript and open file - applescript

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

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

How to get filename to activated application within applescript?

I am creating an applescript that opens terminal, I am dragging a file onto the script (using automater) and want the directory/name of file to be fed into my terminal application
on run {input, parameters}
tell application "Terminal"
activate
do script with command "qpdf --qdf arg1 output.pdf"
end tell
end run
You don't need to use Automator. Save this script as an application instead...
on open of droppedItems
tell application "Terminal"
if not (exists window 1) then reopen
activate
end tell
repeat with anItem in droppedItems
set itemPath to POSIX path of anItem
tell application "Terminal"
do script "qpdf --qdf " & quoted form of itemPath & " output.pdf" in window 1
-- do script "echo " & quoted form of itemPath in window 1
end tell
end repeat
end open

Save ppt as pdf via applescript with Preview.app

I write script to convert xls to pdf with applescript.
Here is my code:
set the_file to (choose file)
tell application "Preview"
activate
open the_file
end tell
tell application "System Events"
tell process "Preview"
keystroke "p" with command down
tell front window
UI elements
end tell
end tell
end tell
When i run script, i have error in
keystroke "p" with command down
whats wrong?
You need the keyword using instead of the keyword with. So, the erroneous line should read:
keystroke "p" using command down

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

Using AppleScript to choose a file in Safari

I am trying to write some automation code (primarily in Ruby Selenium). At some point, a file chooser is opened in Safari so that the user can select a file for upload. Selenium cannot handle this, but I think AppleScript should be able to. I am new to AppleScript and haven't been able to find any boilerplate code of someone automating a file chooser dialog. I'm reading through the AppleScript docs, but any ideas would be most helpful.
Some more searching and I found a great answer here: Applescript file dialog with UI scripting
Here's what I ended up using:
on run argv
tell application "Safari"
activate
-- Usage check
set argc to count argv
if argc is not greater than 0 then
return "Usage: SafariFileChooser file_name [window_name]"
end if
-- The file we will choose to open
set file_name to item 1 of argv
-- Flip to the named window, if specified
if argc is equal to 2 then
set window_name to item 2 of argv
set flip_count to index of window window_name
repeat (flip_count - 1) times
activate
tell application "System Events" to keystroke "`" using command down
end repeat
end if
-- Interact with the dialog using System Events (thanks mcgrailm)
tell front window
activate
tell application "System Events"
keystroke "g" using {shift down, command down}
keystroke file_name
delay 1
keystroke return
delay 1
keystroke return
end tell
end tell
end tell
return 0
end run
Another option I just discovered is to specify the directory using the command-line:
do shell script "defaults write com.apple.Safari NSNavLastRootDirectory /path/to/directory"
This way you can do slightly less in UI scripting. Run this command before you open the file chooser, and it will put you into the directory specified. Include all the files you need in this directory, and you can just script command+a to select them all, and return.

Resources