Applescript: using both "on open" and "choose file" - applescript

Is there a way to use both "on open" and "choose file" in my script without having to repeat the action code as in my example below?
on open myMovie
tell application "Quicktime Player 7"
open myMovie
end tell
end open
set myMovie to choose file
tell application "Quicktime Player 7"
open myMovie
end tell

on open myMovies
repeat with aMovie in myMovies
tell application "QuickTime Player 7" to open aMovie
end repeat
end open
on run
set myMovie to choose file
tell application "QuickTime Player 7" to open myMovie
end run

Another approach is to call the script's open handler from the run handler:
on open theFiles
tell application "QuickTime Player 7" to open theFiles
end open
tell me to open (choose file)

Related

AppleScript - What's wrong with my quotes?

Hello and thanks for your help,
This is driving me crazy! I keep having syntax errors for simple stuff like just opening a file.
I get > Syntax Error - Expected end of line, etc. but found “"”.
What am I doing wrong? It's the same quotes as the ones for Finder...
tell application "Finder"
set theFile to selection
open theFile with application "QuickTime Player"
end tell
Here is the corrected script[1]:
tell application "Finder"
set theFile to selection
open theFile using application file "QuickTime Player" of folder "Applications" of startup disk
end tell
This can be simplified by specifying the application by bundle ID instead of location, in which case Finder will find it for you:
open theFile using application file id "com.apple.QuickTimePlayerX"
Or, if you want to do stuff with the file(s) once they’re opened:
tell application "Finder"
set theFiles to selection as alias list
end tell
if theFiles is {} then error "No files selected."
tell application "QuickTime Player"
activate -- (ensures any "can't open file" error dialogs are visible)
open theFiles
-- do other stuff here
end tell
--
[1] The syntax error is due to the special behavior of with/without parameter labels, which expect to be followed by a single keyword (or comma-separated keywords). AppleScript reads the command as open theFile using application—and since a command can’t be immediately followed by an expression, in this case a literal string, it reports a (frustratingly unhelpful) syntax error.

Running video with applescript doesn't work

The following applescript is meant to open a video file using quicktime player from a file path but I'm not sure why it's not working as it comes up with vague yet menacing errors that are listed as "unknown." Please help fix it. Thanks.
tell application "QuickTime Player"
set theMovie to open file ":Users:User:Desktop:Script:Video.mp4"
tell theMovie
set the presenting to true
set the looping to true
play
end tell
end tell
An HFS path must start with the name of a volume.
set theMovie to open file "Macintosh HD:Users:User:Desktop:Script:Video.mp4"
But there is a relative way
set moviePath to (path to desktop as text) & "Script:Video.mp4"
tell application "QuickTime Player"
set theMovie to open file moviePath
...

applescript to close pdf window

I have an applescript that looks like this:
repeat
tell application "Adobe Reader"
open "filepath/name.pdf"
end tell
delay (60)
tell application "Adobe Reader"
open "filepath/name1.pdf"
end tell
delay (60)
tell application "Adobe Reader"
open "filepath/name2.pdf"
end tell
delay (60)
end repeat
I want to be able to close pdf windows after they have been opened. The issue is that these pdfs reside on a share and users have the ability to update them. The script will only display the updated pdf if it is stopped and restarted. I do not want to have to do this manually. How can I do this?
Here's a solution, which makes Preview scriptable and then continues to open and close a file of your choice.
-- http://www.macworld.com/article/1053391/previewscript.html
on addAppleScriptFeatures()
try
tell application "Finder"
set the Preview_app to (application file id "com.apple.Preview") as alias
end tell
set the plist_filepath to the quoted form of ¬
((POSIX path of the Preview_app) & "Contents/Info")
do shell script "defaults write " & the plist_filepath & space ¬
& "NSAppleScriptEnabled -bool YES" with administrator privileges
do shell script "chmod -R 755 " & the quoted form of (POSIX path of the Preview_app) with administrator privileges
return true
on error myErr number MyNr
display dialog myErr & " (" & MyNr & ")." buttons {"OK"} default button "OK" with icon 0
return false
end try
end addAppleScriptFeatures
if addAppleScriptFeatures() then
set f to choose file
tell application "Preview"
activate
open f
delay 5 -- short for testing
close window 2
end tell
end if

AppleScript droplet that moves file

trying to make an AppleScript droplet that moves a file to a given folder when the file is dragged to the droplet; not sure how to tell the script that the file I just dragged is the one I want to move; I tried this but it doesn't work:
on open dragged_file
tell application "Finder"
move dragged_file to "Macintosh HD:Users...etc."
end tell
end open --script runs but doesn't move file
Try:
on open of theFiles
tell application "Finder"
move theFiles to folder "Macintosh HD:Users...etc."
end tell
end open
To use a droplet, you have to introduce a loop repeat with currentitem in draggeditem
now your script
And then end repeat
for your exemple
on open dragged_file
repeat with currentitem in dragged_file
tell application "Finder"
move dragged_file to folder "Macintosh HD:Users...etc."
end tell
end repeat
end open --script runs but doesn't move file

Applescript to make new folder

I Want to make a new Folder command in apple script
Why dosent this script work?
tell application "Finder"
activate
end tell
tell application "System Events"
tell process "Finder"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
click menu item "New folder"
end tell
end tell
end tell
end tell
end tell
You can do it more directly with AppleScript:
tell application "Finder"
set p to path to desktop -- Or whatever path you want
make new folder at p with properties {name:"New Folder"}
end tell
I don't know if running bash commands within AppleScript is cheating, but you can also do:
do shell script "mkdir ~'/Desktop/New Folder'"
Which is useful when you need to create sub folders on-the-fly when they don't exist yet:
do shell script "mkdir -p ~'/Desktop/New Folder/Bleep/Bloop'"
tell application "Finder"
activate
end tell
tell application "System Events"
tell process "Finder"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
click menu item "new folder"
end tell
end tell
end tell
end tell
end tell
--you capitalize the N in new folder the new folder button is not capped.
NOTE: This can fail for two reasons;
(1) '~' trapped in singlequote won't parse.
(2) space in '/New Folder/' will break the path.
do shell script "mkdir -p '~/Desktop/New Folder/Bleep/Bloop'"
SOLVED:
do shell script "mkdir -p ~/Desktop/" & quoted form of "New Folder/Bleep/Bloop"
You can directly with an applescript script by simulating keystroke on ("N" and command and shift) this will create a new folder on the desktop or in the open Finder window.
Below the script, you can test it in the script editor
tell application "System Events" to tell process "Finder"
set frontmost to true
keystroke "N" using {command down, shift down}
end tell
Your script works if you add under "tell process" Finder "
"set frontmost to true"
Which give
tell application "System Events"
tell process "Finder"
set frontmost to true
tell menu bar 1
tell menu bar item "File"
tell menu "File"
click menu item "New folder"
end tell
end tell
end tell
end tell
end tell
tell application "Finder"
set thepath to alias "Macintosh HD:Users:JasonMagnuson:Documents:" as text
make new folder at thepath with properties {name:"nov_archive"}
end tell

Resources