How to tell Finder to select/reveal multiple files in AppleScript - applescript

Using the following code:
tell application "Finder" to reveal every item of theFiles
Works when theFiles contains a single file, but when it contains multiple files, I get:
error "Finder got an error: AppleEvent handler failed." number -10000
What am I doing wrong? I simply want to highlight a list of files in Finder.

This seems to work:
tell application "Finder" to reveal theFiles
An example I was looking at shows "... reveal every item of ...". I'm not sure if they're in error or something changed with AS.

this seems to work fine
tell application "Finder"
set theFiles to entire contents of folder of (choose folder)
reveal every item of theFiles
end tell

Related

Non-recursive folder actions with applescript

I have a Folder Action watching a folder to run an Applescript whenever a file is added to it.
The Applescript tells my thumbnail generator app to process the images. This works fine. The app creates a subfolder, and places the thumbnails inside.
The issue is that the Folder Action gets triggered for each image made in the subfolders, and it makes a thumbnail of the thumbnails in yet another subfolder.
This happens only once for some reason, but its still too much.
Is there a way I can set a Folder Action to ignore files added to its subfolders? I need to maintain this directory structure where the thumbnail generator outputs into a child folder.
Here's the script:
on adding folder items to this_folder after receiving added_items
repeat with eachItem in added_items
tell application "ThumbsUp" to open eachItem
end repeat
end adding folder items to
On another forum someone suggested this instead:
on adding folder items to this_folder after receiving added_items
repeat with eachItem in added_items
tell application "Finder" to set itsParent to (container of eachItem) as alias
if itsParent is this_folder then tell application "ThumbsUp" to open eachItem
end repeat
end adding folder items to
which should make ThumbsUp not run on items in the subfolder, but for some reason it continues with the same behaviour.
How can I stop folder action from running on subfolders?
Someone on another forum posted script as an answer, which works.
The problem is because ThumbsUp creates the subfolder, and that subfolder gets added to the list of files to open. So this script fixes that
on adding folder items to this_folder after receiving added_items
repeat with eachItem in added_items
tell application "System Events" to set itsKind to kind of eachItem
if not (itsKind is "Folder") then tell application "ThumbsUp" to open eachItem
end repeat
end adding folder items to
Folder Actions do not normally recurse, so I suspect the problem is that the "ThumbsUp" application is designed to process folders of images as well as individual images. When it creates a folder to store the thumbnails, that folder is seen as 'added' to the watched folder, and is sent to the script and back to ThumbsUp. Open ThumbsUp and see if you can set the output folder to a different location. If that doesn't work, try excluding folders from being processed in your script:
on adding folder items to this_folder after receiving added_items
tell application "System Events"
repeat with an_item in added_items
set disk_item to disk item (POSIX path of an_item)
if class of disk_item is not folder then
tell application "ThumbsUp" to open disk_item
end if
end repeat
end tell
end adding folder items to

How can I open an MP3 with quicktime player using applescript?

I have the following code to open the files, but they all open with iTunes:
tell application "Finder"
set the_files to get every file of folder ("Macintosh
SSD:Users:myusername:Desktop:DJ:Music:Sort")
end tell
repeat with theItem in the_files
tell application "QuickTime Player"
open theItem
end tell
end repeat
Thanks!
AS syntax can be very misleading. While your open command is inside a tell application … end tell block, that doesn’t necessarily mean it will be sent to that application. If the command’s direct parameter happens to be a reference to objects in another app (in this case, a reference to a document file object in Finder) AS will send it to that app instead.
Use set the_files to get every file of folder (…) as alias list. That will get you a list of AppleScript alias values, instead of a list of Finder references (which any app other than Finder wouldn’t understand anyway).

Listing folder in /Users

Using Applescript, I have been able to successfully list the folders on my desktop as a selection using this code:
set the_folder to (path to desktop)
tell application "Finder"
set foldernames to name of every folder of entire contents of the_folder
end tell
set theChosenOne to choose from list folder names
However, when I attempt to do the same for the /Users folder:
set Users to "/Users"
set the_folder to Users
tell application "Finder"
set foldernames to name of every folder of entire contents of the_folder
end tell
set theChosenOne to choose from list foldernames
It returns this error: error "Can’t get entire contents of \"/Users\"." number -1728 from «class ects» of "/Users"
Searched on that error, but not finding much info. Thanks for any help you can give me with this one.
The error occurs because the Finder does not support slash separated POSIX paths.
But there is a simpler solution. path to users folder return an alias reference to the folder /Users which can be used directly.
set the_folder to path to users folder
tell application "Finder"
set foldernames to name of every folder of entire contents of the_folder
end tell
set theChosenOne to choose from list foldernames
Caveat: Be aware that entire contents is very slow. After 2 minutes you will get an Apple Event timed out error. You might wrap the Finder tell block in a with timeout block. However I'd recommend find or mdfind of the shell which are incredibly much faster. And most likely you will get also an access privileges violation error.

Dynamic Slideshow using Applescript

I have a folder of images that gets updated from a camera taking pictures periodically throughout the day. I'm trying to write some applescript that will create a slideshow from a folder of images but also update as more are added without having to rerun the script. I started out trying to do quick look but couldn't get that working. Any ideas on how best to tackle this?
UPDATE
this is what I have hacked together so far:
tell application "Finder" to set the_folder to get folder (choose folder)
tell application "Finder" to open the_folder
tell application "System Events"
tell process "Finder"
key code 124
keystroke "a" using command down
keystroke " " using option down
end tell
end tell
I don't believe this works if I add photos behind the scenes though.
This is what I came up with and it works perfectly for what I need.
tell application "Finder" to set the_folder to get folder (choose folder)
tell application "Finder" to open the_folder
tell application "System Events"
tell process "Finder"
key code 124
keystroke "a" using command down
keystroke " " using option down
end tell
end tell
repeat while true
delay 60
tell application "System Events"
tell process "Finder"
keystroke "a" using command down
end tell
end tell
end repeat
A few caveats... since it is using quick look, you can't really do anything on the computer while this is running since it can't have any other app activate while it is running (quick look closes when this happens). Also the repeat section is required to get quick look to pickup the new additions to the directory without losing focus. Pretty nasty stuff, but I couldn't really find another easy way to do it!

Applescript simple open save file snippet?

I just need a snippet to open and then save a file. It's an illustrator PDF with linked artwork, problem being the linked files do not update unless I open then save the file and I am having to do this sooooo many times it's making my brain explode.
Should be straightforward. I tested the following snippet in Illustrator CS3 (13.0.2) on Mac OS X 10.6.8.
set theFiles to choose file with prompt "Select the files" of type {"pdf"} with multiple selections allowed
tell application "Adobe Illustrator"
repeat with theFile in theFiles
open theFile without dialogs
save current document in theFile as pdf
end repeat
end tell
The choose file command (which prompts the user to locate one or more files) is just one way of obtaining a list of files. You could just as well use
tell app "Finder" to set theFiles to every document file of folder "Macintosh HD:Full:Path:To:Your:Folder" whose file type begins with "pdf"
if you want to get all PDF files inside a specific folder.

Resources