Save ppt as pdf via applescript with Preview.app - applescript

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

Related

In MacOSX, can't get applescript+automator service working to send text from any application to "Sublime Text 3"

Want to send text from any application to "Sublime Text 3" and this is how I am thinking of doing it
Create an "automator" service
Set a shortcut for it
Call shortcut from app
I am able to create the "automator" service and it works fine when tested from within "automator" (by means of the "Get Text" action)
But, when I trigger shortcut from application, "Sublime Text" does not open new tab with the selected text (and does nothing)
This is how I am setting up the "automator" service
Service receives selected text in any application
Copy to Clipboard
Run this applescript
on run {input, parameters}
tell application "System Events"
set frontmost of process "Sublime Text" to true
tell application "System Events" to keystroke "n" using command down
tell application "System Events" to keystroke "v" using command down
end tell
end run
Appreciate the help, thanks
The following example AppleScript code works for me:
on run {input, parameters}
tell application "Sublime Text" to activate
delay 1
tell application "System Events"
keystroke "n" using command down
delay 0.5
keystroke "v" using command down
end tell
end run
Note that the value of the delay commands may or may not need to be adjusted for timing on your system.

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

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

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