applescript differentiate file from folder

Discussion in 'Mac Programming' started by dancks, Jun 22, 2013.

  1. macrumors member

    dancks

    Joined:
    Nov 8, 2009
    #1
    I am making a smart folder similar to a symbolic link that whenever a folder is created or something gets saved, its uploaded to my server. the folder structure is to match exactly what is on the server. Every folder will have this script attached:

    Code:
    -- https://discussions.apple.com/thread/1485146?start=0&tstart=0
    -- https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/osascript.1.html
    -- http://forums.macrumors.com/showthread.php?t=1089834
    on run argv
    	set FolderPath to current Folder
    	set thename to name of FolderPath
    	if thename = "place_on_server"
    		try
    			tell application "System Events"
    				do shell script "HD:users:Jason Dancks:Desktop:place_on_server:uploadSO"&argv
    			end tell
    		end try
    	else
    		set thenext to name of container of FolderPath":alertscriptrev.scpt"
    		try
    			do script thenext&argv&thename
    		end try
    	end if
    end run
    on adding folder items to this_folder after receiving added_items
    	set lastchar to 
    	try
    		tell application "System Events"
    			if added_items --right here. I thought I would look for "/"
                                                  --as the last character to see if folder or file
    			do shell script "HD:users:Jason Dancks:Desktop:place_on_server:uploadSO"&"FILE"&this_folder&"/"&added_items
    		end tell
    	end try
    end adding folder items to
    
    I'm having problems with the last

    the script grabs the name of the directory or file and recursively calls the same applescript attached to a containing folder up until it reaches the parent folder: place_on_server. Then it calls the shell script I'm using for uploads. I'm doing this because... I need the path, and I didn't think to just echo the path itself. whatever. learning experience. Problem is I have no idea how I would differentiate between a folder or a file in applescript. Any ideas?
     
  2. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    Remember that added_items is a list of aliases. Loop through your list and check the kind property of your item to see if it's a folder.
     

    Attached Files:

  3. thread starter macrumors member

    dancks

    Joined:
    Nov 8, 2009
    #3
    Better late than never. Thank you very much. I couldn't figure out how the recursion should work when the script is simply attached as a folder action and not present so I abandoned that and went to traditional path

    so instead of ./myscript a b c d test.txt
    its now
    ./myscript /users/me/Desktop/place_on_server/a/b/c/d/test.txt

    but I still need to figure out the POSIX (I'm guessing that *nix) path.

    so revised, script is:

    Code:
    -- https://discussions.apple.com/thread/1485146?start=0&tstart=0
    -- https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/osascript.1.html
    -- http://forums.macrumors.com/showthread.php?t=1089834
    on adding folder items to this_folder after receiving added_items
    	set FolderPath to POSIX path to this_folder
    	set the_name to the name of added_items
    	set isfile to kind of added_items
    	tell application "System Events"
    		try
    			set check to the name of this_folder as string
    			if check = "place_on_server"
    				if isfile = "file"
    					do shell script "HD:users:Jason Dancks:Desktop:place_on_server:uploadSOrev"&"F"&the_name
    				else
    					set FolderPath to path to added_items
    					attach action to (alias FolderPath) using "HD:users:Jason Dancks:Desktop:place_on_server:alertscriptrev.scpt"
    					do shell script "HD:users:Jason Dancks:Desktop:place_on_server:uploadSOrev"&"DIR"&the_name
    				end if
    			else
    				if isfile = "file"
    					do shell script "HD:users:Jason Dancks:Desktop:place_on_server:uploadSOrev"&"F"&FolderPath&the_name
    				else
    					set FolderPath to POSIX path to added_items
    					do shell script "HD:users:Jason Dancks:Desktop:place_on_server:uploadSOrev"&"DIR"&FolderPath
    					set FolderPath to path to added_items
    					attach action to (alias FolderPath) using "HD:users:Jason Dancks:Desktop:place_on_server:alertscriptrev.scpt"
    				end if
    			end if
    		end try
    	end tell
    end adding folder items to
    
     

Share This Page