Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

VideoBeagle

macrumors 6502a
Original poster
Aug 17, 2010
823
18
App Q&A testing by request.
I'm making a simple folder action that will Open files placed into it.
(I'll be using this in a dropbox folder eventually).

My code works as is, but I would like to limit it to only activating files with certain file extensions (The commented out lines).

I've tried variations I've found online, using "Name", using a list and comparing it to that, but I think I'm missing something fundamental
.
Code:
on adding folder items to this_folder after receiving these_items

    repeat with i from 1 to number of items in these_items
        set this_item to item i of these_items as alias
        if this_item is not ".DS_Store" then
            --if name extension of this_item is "txt" then

            tell application "Finder" to open this_item
           
            --end if
        end if
    end repeat
end adding folder items to
 
Here's the example from the Folder Actions Reference section of the ASLG. Notice the use of the info for command.

Code:
on adding folder items to this_folder after receiving these_items

    tell application "Finder"

        if not (exists folder "Done" of this_folder) then

            make new folder at this_folder with properties {name:"Done"}

        end if

        set the destination_folder to folder "Done" of this_folder as alias

        set the destination_directory to POSIX path of the destination_folder

    end tell

    repeat with i from 1 to number of items in these_items

        set this_item to item i of these_items

        set the item_info to info for this_item

        if this_item is not the destination_folder and ¬

            the name extension of the item_info is not in {"zip", "sit"} then

            set the item_path to the quoted form of the POSIX path of this_item

            set the destination_path to the quoted form of ¬

                (destination_directory & (name of the item_info) & ".zip")

            do shell script ("/usr/bin/ditto -c -k -rsrc --keepParent " ¬

                & item_path & " " & destination_path)

        end if

    end repeat

end adding folder items to

Special Considerations
Because info for returns so much information, it can be slow, and because it only works on one file at a time, it can be difficult to use. The recommended technique is to use System Events or Finder to ask for the particular properties you want.
 
Ok.. the name extension route might be more than is necessary.

this "The recommended technique is to use System Events or Finder to ask for the particular properties you want." I'm guessing holds the clue to what I'm looking for.

Goals for this script:
*add the file extension if switch
*delete the file (or move to trash) after it's run.

Edit: Jotting this down as a log of my thoughts, as I'm not at the machine right now to try, but maybe something like
IF last 3 characters of name of item = "txt" then
(written in proper script :) )

Edit 2:
So far so bad...more googling after Star Wars
Code:
           if (characters -3 thru -1 of this_item as text) is "txt" then
               beep 3
           end if
 
Last edited:
I'm making a simple folder action that will Open files placed into it.
(I'll be using this in a dropbox folder eventually).

My code works as is, but I would like to limit it to only activating files with certain file extensions (The commented out lines).

I've tried variations I've found online, using "Name", using a list and comparing it to that, but I think I'm missing something fundamental
.
Code:
on adding folder items to this_folder after receiving these_items

    repeat with i from 1 to number of items in these_items
        set this_item to item i of these_items as alias
        if this_item is not ".DS_Store" then
            --if name extension of this_item is "txt" then

            tell application "Finder" to open this_item
      
            --end if
        end if
    end repeat
end adding folder items to
The variable this_item in your case refers to an alias object which doesn't have a name extension property. You'll want to obtain an object specifier for a Finder item instead:

Code:
tell application "Finder" to set anItem to item (item i of these_items)

display dialog (name extension of anItem) as string
 
Last edited:
Ok.. I took a holiday break on this then tackled it with some fresh thoughts right now.
This seems to work the way I want it to:

Code:
on adding folder items to this_folder after receiving these_items
    --delay 5
    repeat with i from 1 to number of items in these_items
        set this_item to item i of these_items as alias
        if this_item is not ".DS_Store" then
            
            tell application "Finder"
                set fileext to name extension of this_item
                if fileext is "blah" then
                    open this_item
                    delete this_item
                    --beep 1
                    delay 10
                end if
                
                
            end tell
        end if
    end repeat
    --beep 3
end adding folder items to

(The delay is there to for the running program...some tests look like it prefers a delay between activations, but I'm not 100% sure. BUT, this action is so I can save things to a dropbox folder from my iphone when I'm not a tthe computer and activate things, so I'm not overly concerened about the delay... Also, it's unlikely I'd do multiple savings at once so it's likely moot, but just in case :)

Thoughts?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.