Non-recursive folder actions with applescript - 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

Related

Granting permissions to AppleScript script

I've a simple AppleScript that it's supposed to delete everything in the Downloads folder with exception of selected list of folders passed at the end.
tell application "Finder"
delete (every item of "Downloads" of folder currentUser of folder "Users" of startup disk whose name is not in {"PreciousFolder"})
end tell
On save the AppleScript returns the following error:
You are going to delete every item of the literal string "Downloads" which makes no sense. Add the folder specifier
tell application "Finder"
delete (every item of folder "Downloads" of folder currentUser of folder "Users" of startup disk whose name is not in {"PreciousFolder"})
end tell
or shorter
tell application "Finder"
delete (every item of (path to downloads folder) whose name is not in {"PreciousFolder"})
end tell

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.

How do I Copy files to a USB with apple script that runs on said USB?

I run a computer lab at Denver University. We are having an issue with our backup system, and I need to copy/duplicate all of the files on the (Apple) computers onto a USB that I keep at my desk.
At the moment, I need to plug in a USB into each of the computers and manually duplicate each of the items of the "documents" folder and deposit them into a new folder on my USB. I need an Apple Script that I put on my USB, insert my USB into a computer, activate the app, and it will make a new directory named "(User name) upload", and deposit all of the items into that directory. Here is what I have so far:
tell application "Finder"
set theFolder to disk / Volumes / Lexar / stuff
set Files1 to Users / matanya / documents
tell application "Finder"
try
duplicate file Files1 to theFolder
on error
tell application "Finder"
display dialog "Transfer Failed"
end tell
end try
end tell
one of the issues is that every time I run the script, I get an error that says the the variable "Volumes" is not defined. Another one is that I am afraid that when I plug this script into another computer, it will not find the folder "matanya" that I have in my directory. Is there a way to call it "home" or something?
First of all the Finder accepts only HFS paths – starting with a disk name and colon separated. The folder Volumes doesn't matter when using HFS paths.
path to documents folder points always to the folder Documents of the current user.
set theFolder to "Lexar:stuff:"
set documentsFolder to path to documents folder
tell application "Finder"
if not (exists disk "Lexar") then
display dialog "Insert disk 'Lexar'" buttons "Cancel" default button 1
end if
try
duplicate documentsFolder to folder theFolder
on error
display dialog "Transfer Failed"
end try
end tell
More sophisticated this version creates a folder with the short user name of the current user and duplicates the documents folder to that individual folder
set theFolder to "Lexar:stuff:"
set currentUser to short user name of (system info)
set documentsFolder to path to documents folder
tell application "Finder"
if not (exists disk "Lexar") then
display dialog "Insert disk 'Lexar'" buttons "Cancel" default button 1
end if
if not (exists folder currentUser of folder theFolder) then
make new folder at folder theFolder with properties {name:currentUser}
end if
try
duplicate documentsFolder to folder currentUser of folder theFolder
on error
display dialog "Transfer Failed"
end try
end tell
With launchd you even could run this script automatically whenever the USB drive is inserted.

How to tell Finder to select/reveal multiple files in 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

Resources