Noob - easy one

Discussion in 'Mac Programming' started by irreality, Mar 6, 2014.

  1. irreality macrumors newbie

    Joined:
    Mar 6, 2014
    #1
    If someone out there has the knowhow to quickly write up an applescript for me to do one simple task I would be forever in your debt.

    All it needs to do is change the filename of files added to a folder.
    ( i know how to add the folder action, just not sure how to write the script :/ )

    So it can either do one of the two following:

    Take the parent folders name and add that text to the filename just before the extension.

    Ex. FOLDERX>abcdefg.mov would become FOLDERX>abcdefg_FOLDERX.mov

    or

    Just one where if files are added the text is simply added before the extension.

    so ABCDEFG.mov would become ABCDEFG_ADDTEXT.mov

    the one caveat is that it needs to be set up to be used as a folder action and not having to be manually run every time I add files.

    Thanks oodles. the promise of beer is in your future.

    :D
     
  2. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #2
    I'll tell you what. I need an addition added to my house. I would be forever in debt to you. While you are doing that I can do this for you. :D
     
  3. irreality thread starter macrumors newbie

    Joined:
    Mar 6, 2014
    #3
    You didnt need to be rude about it.

    I get where you are coming from, however I was basing it on my assessment that I know this is a very simple script, and the fact that there are plenty of noobs on these forums asking for very similar requests and not getting snotty answers. With even more much more complicated scripting requests.

    I can see at least 4 on the first page alone.
     
  4. kryten2, Mar 6, 2014
    Last edited: Mar 6, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    Relax. First are we talking about an Applescript folder action with this piece of code :

    Code:
    on adding folder items to this_folder after receiving these_items
    	-- insert actions here
    end adding folder items to
    or a folder action in Automator with a Run Applescript action? I also don't get if files are added the text is simply added before the extension.

    so ABCDEFG.mov would become ABCDEFG_ADDTEXT.mov.

    Where does the ADDTEXT come from?

    Edit : There's a good piece of code to get you going by forum member Red Menace here
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    What OS version does it have to run on?

    I ask because a Folder Action in Automator has the ability to run a shell script, not just an Applescript. This capability of Automator depends on OS version.

    ----------

    It's the immediate parent folder of the file, i.e. the folder that has the folder action.

    From the OP:
     
  6. irreality, Mar 6, 2014
    Last edited: Mar 6, 2014

    irreality thread starter macrumors newbie

    Joined:
    Mar 6, 2014
    #6
    Sorry about that,

    Ive been searching around trying to figure it out for about 3 hours now. tinkering but because Im not all that versed on applescript, and most of the examples that ive found are much more in depth than what i require.

    Really the ADDTEXT could either come from the parent folder, or just as a textstring added directly into the script.

    so wherever i put "ADDTEXT" it would just add whatever text i put in there.

    I found the folder action no problem, its just the altering of the filename that im having issue with.

    This is probably something stupid basic that you would learn in the first class of applescript 101. However, as much as i would like to learn how to code etc. I can just never find the time to sit down and let it sink in.

    I find I know that my biggest issue is I know how to proceed with the steps and structuring. Its the language that Ive never learned.

    MY OS version is 10.6

    Automator is No good unfortunately... I made one to make the change. However It was spotty at working at best.... Kept getting back the below message in the console... Looked around seemed to be a common issue.

    14-03-06 8:19:12 AM [0x0-0x99099].com.apple.AppleScriptRunner 2014-03-06 08:19:12.396 Automator Runner[3749:903] Automator Runner is unable to establish a connection to the delegate.
     
  7. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #7
    Post the code you have been trying. Basically all you need are 3 properties of a Finder item eg name, name extension and container.
     
  8. irreality thread starter macrumors newbie

    Joined:
    Mar 6, 2014
    #8
    Code:
    on adding folder items to the target_folder after receiving these_items
    	tell application "Finder"
    		set name of file to (file) & "ADDTEXT"
    	end tell
    end adding folder items to
    I know its missing something... but im not sure what.
     
  9. kryten2, Mar 6, 2014
    Last edited: Mar 6, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #9
    If you're processing files you'll need a repeat loop. These_items is a list of aliases that identify the items added to the folder. Here's something basic you can try :


    Code:
    on adding folder items to this_folder after receiving these_items
    	tell application "Finder"
    		if not (exists folder "Renamed Files" of this_folder) then
    			set destination_folder to make new folder at this_folder with properties {name:"Renamed Files"}
    		end if
    		set the destination_folder to folder "Renamed Files" of this_folder as alias
    	end tell
    	repeat with i from 1 to number of items in these_items
    		set anItem to item i of these_items
    		if anItem is not the destination_folder then
    			set baseName to do shell script "sed -E 's|:$||;s|.*:||;s|\\.[^.]*$||' <<< " & quoted form of (anItem as string)
    			tell application "Finder"
    				set itemExtension to name extension of anItem
    				set movedItem to move anItem to destination_folder
    				set name of movedItem to baseName & "ADDTEXT" & "." & itemExtension
    			end tell
    		end if
    	end repeat
    end adding folder items to
    
    Note : This will only process files NOT folders.

    or this one :


    Code:
    on adding folder items to this_folder after receiving these_items
    	tell application "Finder"
    		if not (exists folder "Renamed Files" of this_folder) then
    			set destination_folder to make new folder at this_folder with properties {name:"Renamed Files"}
    		end if
    		set the destination_folder to folder "Renamed Files" of this_folder as alias
    	end tell
    	repeat with i from 1 to number of items in these_items
    		set anItem to item i of these_items
    		set anItemInfo to info for anItem
    		if anItem is not the destination_folder and folder of anItemInfo is false then
    			set baseName to do shell script "sed -E 's|:$||;s|.*:||;s|\\.[^.]*$||' <<< " & quoted form of (anItem as string)
    			tell application "Finder"
    				set itemExtension to name extension of anItem
    				set movedItem to move anItem to destination_folder
    				set name of movedItem to baseName & "ADDTEXT" & "." & itemExtension
    			end tell
    		else
    			tell application "Finder" to set folderContents to (every file of entire contents of anItem) as alias list
    			repeat with j from 1 to number of items in folderContents
    				set aFolderItem to item j of folderContents
    				set baseName to do shell script "sed -E 's|:$||;s|.*:||;s|\\.[^.]*$||' <<< " & quoted form of (aFolderItem as string)
    				tell application "Finder"
    					set aFolderItemContainer to name of aFolderItem's container
    					set aFolderItemExtension to name extension of aFolderItem
    					set name of aFolderItem to baseName & "_" & aFolderItemContainer & "." & aFolderItemExtension
    				end tell
    			end repeat
    		end if
    	end repeat
    end adding folder items to
    
    Try it out by dropping some files and the attached example folder into your watched folder and see what the result is.
     

    Attached Files:

  10. irreality thread starter macrumors newbie

    Joined:
    Mar 6, 2014
    #10
    I tried the first one, It did exactly what i was looking for... Thanks so much ! ... It did however take out the "." between the filename and extension. But I just ended up working it into the name change itself.

    I will try the other one out tomorrow and see how she goes... but i think that moves the renamed files to another folder.... I dont need that bit.

    However I know this problem must be deeper than the code itself, something to do with the OS. But the script gets "throttled" wait 8 secs etc. Coming from the console... same kind of thing happened when i was using the automator actions.

    which means if i drop a file in when its "throttling" it doesnt respond and doesnt change the name.... Which kind of sucks. Because Im feeding Files saved from photoshop actions into these folders.

    Anybody know a way to "fix" that issue ? Once again kryten... thank you so much, maybe some day you can break it down for me and tell me exactly what each piece is doing... so i have a better understanding.
     
  11. kryten2, Mar 6, 2014
    Last edited: Mar 6, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #11
    The first script also moves the file(s). Why? Because a well written Folder Action script leaves the hot folder empty. This avoids repeated application of the action to the same files, and allows Folder Actions to perform more efficiently. Let's take as example a targeted folder named FolderAction and you drop a file named example.txt in it. Here's what happens :

    1. A folder named "Renamed Files" will be made in folder FolderAction if it doesn't exist.
    2. Loop through the number of items dropped. In the example one item. For each item do:
    3. Get baseName (eg without the .txt) from the file example.txt. Result:baseName=example
    4. Get extension (eg txt) from the file example.txt. Result:itemExtension=txt
    5. Move file example.txt to the "Renamed Files" folder.
    6. Rename example.txt in the "Renamed Files" folder by concatenating(eg &) the various parts to baseName & "ADDTEXT" & "." & itemExtension eg
      exampleADDTEXTdottxt

    Can't really do anything about the throttling issue. A possible fix would be to try a LaunchAgent.
     

Share This Page