Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

I need help with automator or apple script and/or both

Hello AppleScript/Automator Gurus

Firstly, I just started using Automation this week with Automator but I've ran into a wall.. x.x I'm hoping I can resolve this by means of AppleScripting.

i was trying to sort files into existing folders but i couldn't figure out a way to add multiple actions in to one work flow. all the simple video tutorials i found on the web were about a single action find finder items and then move the finder items. in one work flow then if i had multiple files i had to modify the name and save another work flow, well it work for about 3 seconds, after making more then 4 work flows and adding them to the same master folder it started to jam up or not work at all. the output inconsistency was to great to ignore so im trying for find another way to go about it.

Below is a video link of what I'm trying to do. hopefully some one can help me build something dynamic so i ca just drop certain criterial and run it.


http://www.youtube.com/watch?v=V9poSnMMqCo&feature=youtu.be


if any one need additional information let me know and i will try to describe it as best as possible

applescript automator-OTHER, Mac OS X (10.6.8)

Posted on Oct 30, 2012 9:45 AM

Reply
11 replies

Nov 1, 2012 6:51 PM in response to applescript_problems

Hi,


I must tell you that I don't really understand spoken English, but I understand English better when I read it.


I must tell you that the folder actions are not reliable, but it depends on many factors.

1- Depending on the time to copy the item dropped in this folder.

2- Depending on the number of files dropped or created at the same time in this folder.

3- Depending on the time interval between the creation of two images.

4- Depending on the duration that take the script to complete its task.

5- Depending on the number of script or workflow activated to this folder action.

6- There are slightly more problem with Automator.

7- Depending on unknown factors on some systems.


Just testing to see if some script works for you.




Here's what I understood.

The script must search a subFolder by the beginning of its name, but it can be in different level.


Example: the fileName is "bigmac_ld_4panel.png"

The script must find (recursively descends the directory tree) a subfolder whose name starts with "4_panel_"


After that, I guess the name of the destination folder is the exact name ("ld" in this example).

I also assume that the "ld" folder is in the "4_panel_xxxxxx" folder (not in another level).


Here is the script, use it as a AppleScript folder action, but not in Automator :

---------------------------

property nameList : {"_4panel.png", "_5panel.png"} -- the end of the fileName to check
property subFoldersList : {"4_panel_", "5_panel_"} -- the beginning of the folderName to search

on adding folder items to masterFolder after receiving these_items
      set masterPath to quoted form of POSIX path of masterFolder
      repeat with tFile in these_items
            tell application "System Events" to set tName to name of tFile
            set {subName, destName} to my checkName(tName)
            if destName is not "" then -- this file name is in the list
                  --  search (depth levels) the subfolder whose name start with subName and that folder contains the destination folder.
                  set f to do shell script "/usr/bin/find " & masterPath & " -type d -regex " & (quoted form of (".*/" & subName & ".*/" & destName))

                  -- if it didn't find anything or it found two or more subfolders then do nothing
                  if f is not "" and (count of paragraphs of f) = 1 then
                        set destFolder to (POSIX file f) as alias
                        tell application "Finder" to move tFile to destFolder with replacing --move this file
                  end if
            end if
      end repeat
end adding folder items to

on checkName(tName)
      set tc to count nameList
      repeat with i from 1 to tc
            if tName ends with (item i of nameList) then -- this Name is in the list
                  -- return the beginning of the subFolder name and the name of the destination folder
                  return {item i of subFoldersList, my getNameOfDestinationFolder(tName, item i of nameList)}
            end if
      end repeat
      return {"", ""} -- this name is not in the list
end checkName

on getNameOfDestinationFolder(thisName, theEndOfName) -- e.g. this Name = "bigmac_ld_4panel.png" 
      set tName to text 1 thru -((count theEndOfName) + 1) of thisName -- get text before "_4panel.png" --> "bigmac_ld"
      if "_" is not in tName then return "" -- no underscore
      set {oTID, text item delimiters} to {text item delimiters, "_"}
      set r to last text item of tName -- get text after last underscore -->  ld
      set text item delimiters to oTID
      return r
end getNameOfDestinationFolder

---------------------


You must add the names in the lists (first line and second line of this script) according to your needs.

The underscore at beginning of each item in the first list is important, It's to find the name of the destination folder.


The items in these two lists must absolutely match (the index).

Example: the third item in the first list (the end of the file name) must match the third element in the second list (the beginning of the subfolder name ).



--------------------------------

I don't know if it suits you, but it can be interesting.

I have a small suggestion about your naming convention, the script would be easier and you will not have to fill the lists in the script.

Example :

Instead of renaming the file like this "bigmac_ld_4panel.png", If you rename like this "bigmac;ld;4_panel_.png" or "bigmac_;ld;4_panel_.png".


"4_panel_" is the exact text to search for a subdirectory whose name starts with that.

"ld" is the exact name of the destinaton folder.


So, the script looks in the file name:

The text between the semicolon character and the name extension. (It's the beginning of the subfolder name).

The text between the two semicolon characters. (It's the name of the destination folder).


Here is the script for this naming convention.

---------------------------

property nameExtensionList : {"png", "tif"} -- list of name extension that the script will be processed..

on adding folder items to masterFolder after receiving these_items
      set masterPath to quoted form of POSIX path of masterFolder
      repeat with tFile in these_items
            tell application "System Events" to set {name:tName, name extension:nExtension} to tFile
            if nExtension is in nameExtensionList then
                  set {destName, subName} to my checkName(tName, nExtension)
                  if destName is not "" and subName is not "" then
                        --  search (depth levels) the subfolder whose name start with subName and that folder contains the destination folder.
                        set f to do shell script "/usr/bin/find " & masterPath & " -type d -regex " & (quoted form of (".*/" & subName & ".*/" & destName))

                        -- if it didn't find anything or it found two or more subfolders then do nothing
                        if f is not "" and (count of paragraphs of f) = 1 then
                              set destFolder to (POSIX file f) as alias
                              tell application "Finder" to move tFile to destFolder with replacing --move this file
                        end if
                  end if
            end if
      end repeat
end adding folder items to

on checkName(thisName, nameExt) --e.g.  name = "bigmac_;ld;4_panel_.png"
      set r to {"", ""}
      set tBaseName to text 1 thru -((count nameExt) + 2) of thisName -- remove name extension --> "bigmac_;ld;4_panel_
      if ";" is not in tBaseName then return r
      set {oTID, text item delimiters} to {text item delimiters, ";"}
      -- to get text between semicolon character -->  "ld" and "4_panel_"
      tell (text items of tBaseName) to if (count) > 2 then set r to items -2 thru -1 --  two semicolons minimum
      set text item delimiters to oTID
      return r
end checkName

Nov 1, 2012 10:15 PM in response to Jacques Rioux

I made a small mistake in the regex:

The destination folder may not be within the founded subfolder, but deeper.


Hypothetical example with"bigmac_ld_4panel.png" ---> /masterFolderName/xyz/ccc/4_panel_evm_images/sub_A/ld/

The "ld" folder is within the"sub_A" folder instead of "4_panel_evm_images" folder.


If you don't want it, replace the line that contains "-regex" by this :


set f to do shell script "/usr/bin/find " & masterPath & " -type d -regex " & (quoted form of (".*/" & subName & "[^/]*/" & destName))

Nov 2, 2012 1:00 PM in response to Jacques Rioux

Hi jacques,

Thanks again for your help, i made a written document describing the video.


https://docs.google.com/open?id=0B33s16CVwXdTYXRiZWRRelhuSnc


i will try the script you wrote, hopefully i can get it to function the way i envisioned.


thanks again for all your help.


o by the way i forgot to mention in order to fully view the file you need to download it first, open with any jpeg viewer and zoom in. the file is big enough to read all text.

Nov 2, 2012 3:44 PM in response to applescript_problems

Hi,



So the script had no need to extract the text in the file name.


The script only needs these lists, 1- the end of the name, 2- the full path of the destination folders for each name in the first list.


If the end of the filename is in the first list, the script returns the folder path in the second list (at the same index).


Here is the script:

-----------------------


property nameList : {"ld_4panel.png", "bf_5panel.png", "??secondExtension5panel??", "_ds_dt_.png", "_ss_dt_.png", "_ts_dt_.png"} -- the end of the fileName to check
property destFoldersPath : {"", "", "", "", "", ""} -- drag/drop the dest folder in each item of this list

on adding folder items to masterFolder after receiving these_items
      repeat with tFile in these_items
            set destFolder to my check_end_of_Name(tFile as string)
            if destFolder is not "" then -- this file name is in the first list
                  tell application "Finder" to move tFile to destFolder with replacing --move this file
            end if
      end repeat
end adding folder items to

on check_end_of_Name(t)
      set tc to count nameList
      repeat with i from 1 to tc
            if t ends with (item i of nameList) then return (POSIX file (item i of destFoldersPath)) as alias -- return the dest folder
      end repeat
      return "" -- this name is not in the list
end check_end_of_Name

Nov 6, 2012 10:09 AM in response to Jacques Rioux

thanks for all the great help.

i tested the apple script, it does the job with some small glitches.


when i drop more then 10 or 15 files into the selected folder with the apple script it send most of them to its destination. at the moment i have about 123 files and every time i drop the files in folder with the apple script linked to it would place all of them except for 45 files then i would pull the 45 from that folder and put them back in the folder and it will filter about 30-35. I have to repeat the step untill all files sorted to the required destination.


heres the files and the destination script i have set up.


property nameList : {"_bf_4panel.png", "_ld_4panel.png", "_ss_4panel.png", "_ds_4panel.png", "_ts_4panel.png", "_bf_5panel.png", "_ld_5panel.png", "_ss_5panel.png", "_ds_5panel.png", "_ts_5panel.png", "_bf_dt.png", "_ld_dt.png", "_ss_dt.png", "_ds_dt.png", "_ts_dt.png"} -- the end of the fileName to check


property destFoldersPath : {"/Users/jorgegarcia/Desktop/script_test/4_panel/bf", "/Users/jorgegarcia/Desktop/script_test/4_panel/ld", "/Users/jorgegarcia/Desktop/script_test/4_panel/spacers", "/Users/jorgegarcia/Desktop/script_test/4_panel/spacers", "/Users/jorgegarcia/Desktop/script_test/4_panel/spacers", "/Users/jorgegarcia/Desktop/script_test/5_panel/bf", "/Users/jorgegarcia/Desktop/script_test/5_panel/ld", "/Users/jorgegarcia/Desktop/script_test/5_panel/spacers", "/Users/jorgegarcia/Desktop/script_test/5_panel/spacers", "/Users/jorgegarcia/Desktop/script_test/5_panel/spacers", "/Users/jorgegarcia/Desktop/script_test/drive_thru/bf", "/Users/jorgegarcia/Desktop/script_test/drive_thru/ld", "/Users/jorgegarcia/Desktop/script_test/drive_thru/spacers", "/Users/jorgegarcia/Desktop/script_test/drive_thru/spacers", "/Users/jorgegarcia/Desktop/script_test/drive_thru/spacers"} -- drag/drop the dest folder in each item of this list


on adding folder items tomasterFolderafter receivingthese_items

repeat with tFile in these_items

set destFolder to my check_end_of_Name(tFile as string)

if destFolder is not "" then -- this file name is in the first list

tell application "Finder" to move tFile to destFolder with replacing --move this file

end if

end repeat

end adding folder items to


on check_end_of_Name(t)

set tc to count nameList

repeat with i from 1 to tc

if t ends with (item i of nameList) then return (POSIX file (item i of destFoldersPath)) as alias -- return the dest folder

end repeat

return "" -- this name is not in the list

end check_end_of_Name



When i place a single file to a folder everything works great and theirs no file limits it will take them all with out stoping and sort the files.

if you notice in the script:

file names "_ss_4panel.png", "_ds_4panel.png", "_ts_4panel.png" all go to the same folder.


I don't know how to set it up (write the proper organization), so what i did was just place the path 3 times "/Users/jorgegarcia/Desktop/script_test/4_panel/spacers", "/Users/jorgegarcia/Desktop/script_test/4_panel/spacers", "/Users/jorgegarcia/Desktop/script_test/4_panel/spacers"


I'm assuming theirs a way to tell the script that you can send all 3 file names to one path. at the moment is set up where you have to send each file to a different path but each path is the same location.


is their an efficient way to write this and test to see if it continues to stop on certain files?


Nov 6, 2012 7:45 PM in response to applescript_problems

applescript_problems wrote:


I'm assuming theirs a way to tell the script that you can send all 3 file names to one path. at the moment is set up where you have to send each file to a different path but each path is the same location.


You must use a list that contains lists


property endOfNameList : {"_4panel.png", "_5panel.png", "_dt.png"} -- the end of the fileName to check (3)
property destSub_name : {"4_panel", "5_panel", "drive_thru"} --3 real name of the folder (relative to the first list)
property partNameList : {{"_bf"}, {"_ld"}, {"_ss", "_ds", "_ts"}} -- 3  lists in this list
property destName : {"bf", "ld", "spacers"} -- 3 destination folder name (one name for each list in partNameList)
property destMasterFolder : (POSIX file "/Users/jorgegarcia/Desktop/script_test/") as string

on adding folder items to masterFolder after receiving these_items
      repeat with tFile in these_items
            my check_end_of_Name(tFile)
      end repeat
end adding folder items to

on check_end_of_Name(tFile)
      set tc to count endOfNameList
      set t to tFile as string
      repeat with tEnd in endOfNameList
            if t ends with (contents of tEnd) then
                  set tc to count nameList
                  repeat with i from 1 to tc -- each list in partNameList
                        repeat with each in (list i of partNameList) -- each item in this list
                              if t ends with (each & tEnd) then -- example: each = "_ld", tEnd = "_4panel.png" --> "_ld_4panel.png"
                                    set dest to (destMasterFolder & item i of destSub_name & ":" & item i of destName) as alias
                                    tell application "Finder" to move tFile to dest with replacing --move this file
                                    return
                              end if
                        end repeat
                  end repeat
            end if
      end repeat
end check_end_of_Name





applescript_problems wrote:


is their an efficient way to write this and test to see if it continues to stop on certain files?


The problem is not from the script, but from the process (folders action) that launches scripts.

The process does not list any files dropped in the folder, the process abort the action to restart the script with new parameters, in any case some files will not be processed.



This is what I wrote in my first post, folder actions are not reliable.

Nov 7, 2012 8:26 AM in response to Jacques Rioux

I made ​​a mistake with one of the variable names.

Here is the corrected script.



property endOfNameList : {"_4panel.png", "_5panel.png", "_dt.png"} -- the end of the fileName to check (3)
property destSub_name : {"4_panel", "5_panel", "drive_thru"} --3 real name of the folder (relative to the first list)
property partNameList : {{"_bf"}, {"_ld"}, {"_ss", "_ds", "_ts"}} -- 3  lists in this list
property destName : {"bf", "ld", "spacers"} -- 3 destination folder name (one name for each list in partNameList)
property destMasterFolder : (POSIX file "/Users/jorgegarcia/Desktop/script_test/") as string

on adding folder items to masterFolder after receiving these_items
      repeat with tFile in these_items
            my check_end_of_Name(tFile)
      end repeat
end adding folder items to

on check_end_of_Name(tFile)
      set t to tFile as string
      repeat with tEnd in endOfNameList
            if t ends with (contents of tEnd) then
                  set tc to count partNameList
                  repeat with i from 1 to tc -- each list in partNameList
                        repeat with each in (list i of partNameList) -- each item in this list
                              if t ends with (each & tEnd) then -- example: each = "_ld", tEnd = "_4panel.png" --> "_ld_4panel.png"
                                    set dest to (destMasterFolder & item i of destSub_name & ":" & item i of destName) as alias
                                    tell application "Finder" to move tFile to dest with replacing --move this file
                                    return
                              end if
                        end repeat
                  end repeat
            end if
      end repeat
end check_end_of_Name

Nov 8, 2012 7:30 AM in response to applescript_problems

Hi,


If you edit the script, show what you change, otherwise it is impossible to know why it doesn't work.

But before you change anything in the script, Test it and give us your feedback.




applescript_problems wrote:


when i drop more then 10 or 15 files into the selected folder with the apple script it send most of them to its destination. at the moment i have about 123 files and every time i drop the files in folder with the apple script linked to it would place all of them except for 45 files then i would pull the 45 from that folder and put them back in the folder and it will filter about 30-35. I have to repeat the step untill all files sorted to the required destination.

But you already know that this doesn't work and you want to continue with the folder actions?

Unless you think that the problem comes from the script.


So, as I am not competent enough for this job, I fire myself 🙂 😎.

People more brilliant than me will surely help you with the script, they can better explain you all the details.

Nov 8, 2012 10:27 AM in response to Jacques Rioux

the first script works.

property nameList : {"_bf_4panel.png", "_ld_4panel.png", "_ss_4panel.png", "_ds_4panel.png", "_ts_4panel.png", "_bf_5panel.png", "_ld_5panel.png", "_ss_5panel.png", "_ds_5panel.png", "_ts_5panel.png", "_bf_dt.png", "_ld_dt.png", "_ss_dt.png", "_ds_dt.png", "_ts_dt.png"} -- the end of the fileName to check


property destFoldersPath : {"/Users/jorgegarcia/Desktop/script_test/4_panel/bf", "/Users/jorgegarcia/Desktop/script_test/4_panel/ld", "/Users/jorgegarcia/Desktop/script_test/4_panel/spacers", "/Users/jorgegarcia/Desktop/script_test/4_panel/spacers", "/Users/jorgegarcia/Desktop/script_test/4_panel/spacers", "/Users/jorgegarcia/Desktop/script_test/5_panel/bf", "/Users/jorgegarcia/Desktop/script_test/5_panel/ld", "/Users/jorgegarcia/Desktop/script_test/5_panel/spacers", "/Users/jorgegarcia/Desktop/script_test/5_panel/spacers", "/Users/jorgegarcia/Desktop/script_test/5_panel/spacers", "/Users/jorgegarcia/Desktop/script_test/drive_thru/bf", "/Users/jorgegarcia/Desktop/script_test/drive_thru/ld", "/Users/jorgegarcia/Desktop/script_test/drive_thru/spacers", "/Users/jorgegarcia/Desktop/script_test/drive_thru/spacers", "/Users/jorgegarcia/Desktop/script_test/drive_thru/spacers"} -- drag/drop the dest folder in each item of this list


on adding folder items tomasterFolderafter receivingthese_items

repeat with tFile in these_items

set destFolder to my check_end_of_Name(tFile as string)

if destFolder is not "" then -- this file name is in the first list

tell application "Finder" to move tFile to destFolder withreplacing --move this file

end if

end repeat

end adding folder items to


on check_end_of_Name(t)

set tc to count nameList

repeat with i from 1 to tc

if t ends with (item i of nameList) then return (POSIX file (item i ofdestFoldersPath)) as alias -- return the dest folder

end repeat

return "" -- this name is not in the list

end check_end_of_Name



the one that didn't work at all was the 2nd script.


property endOfNameList : {"_4panel.png", "_5panel.png", "_dt.png"} -- the end of the fileName to check (3)
property destSub_name : {"4_panel", "5_panel", "drive_thru"} --3 real name of the folder (relative to the first list)
property partNameList : {{"_bf"}, {"_ld"}, {"_ss", "_ds", "_ts"}} -- 3 lists in this list
property destName : {"bf", "ld", "spacers"} -- 3 destination folder name (one name for each list in partNameList)
property destMasterFolder : (POSIX file "/Users/jorgegarcia/Desktop/script_test/") as string
on adding folder items to masterFolder after receiving these_items
repeat with tFile in these_items
my check_end_of_Name(tFile)
end repeat
end adding folder items to
on check_end_of_Name(tFile)
set t to tFile as string
repeat with tEnd in endOfNameList
if t ends with (contents of tEnd) then
set tc to count partNameList
repeat with i from 1 to tc -- each list in partNameList
repeat with each in (list i of partNameList) -- each item in this list
if t ends with (each & tEnd) then -- example: each = "_ld", tEnd = "_4panel.png" --> "_ld_4panel.png"
set dest to (destMasterFolder & item i of destSub_name & ":" & item i of destName) as alias
tell application "Finder" to move tFile to dest with replacing --move this file
return
end if
end repeat
end repeat
end if
end repeat
end check_end_of_Name


when i attached the 2nd script to the folder it didn't do any of the actions. i think is because the mata data on the original folder got mixed up with the 1st script. I then built a second master folder with out any matadata and attached the 2nd script and it did the same thing. all of the files were on the master folder and never got sorted. i dont have an answer for what is happening.


for the moment being i will use the 1st script it works well when file amouts are small plus it does exactly what i need. i will try and play with the 2nd script to see what the error is...if i get it to work then i will give you an update. and hopefully the second script can be used.


your not incompetent and you did a great job helping me, so dont sell your self short.


thank you again for all your help and if you ever need any design input or help let me know and will do my best to return the favor.

I need help with automator or apple script and/or both

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.