Applescript - Add prefixes in file name to an array

I have a little script I am working on and I am stuck on this feature.


If I have a folder full of files with prefixes like

sm_test.png

sm_test2.png

med_test.png

med_test2.png

lg_test.png

lg_test2.png


I would like to add each unique prefix to an array without any duplicates.

{"sm", "med", "lg"}

OS X Mavericks (10.9)

Posted on Jan 27, 2014 1:49 PM

Reply
3 replies

Jan 27, 2014 2:05 PM in response to CheckYourVector

Here:


set the_array to {}

tell application "Finder"

repeat with this_file in (get files of (choose folder))

set the_name to name of this_file

set the_prefix to items 1 thru ((offset of "_" in the_name) - 1) of the_name as string

if the_array does not contain the_prefix then set the_array to the_array & the_prefix

end repeat

end tell


(98827)

Jan 27, 2014 3:24 PM in response to Niel

Thank you so much Niel! Since you helped me with my other post. I have one last questions and I swear I will leave you alone. Basically I am trying to combine the two.


I have the script creating folders based on the prefixs then I need to move the files to their folders and remove the prefix. It seems to work when there is only one prefix but breaks when there is more then one.


set the_array to {}
tell application "Finder"
          try
                    set theLocation to the selection as alias
          on error
                    set theLocation to (folder of the front window as alias)
          end try
  
          repeat with this_file in (get files of theLocation)
                    set the_name to name of this_file
                    set the_prefix to items 1 thru ((offset of "_" in the_name) - 1) of the_name as string
                    if the_array does not contain the_prefix then set the_array to the_array & the_prefix
          end repeat
  
          repeat with e in the_array
                    if not (exists folder e of theLocation) then make new folder at theLocation with properties {name:e}
          end repeat
  
  
          set this_list to every file of theLocation
          set AppleScript's text item delimiters to {"_"}
          repeat with i in this_list
                    repeat with ee in the_array
                              if (name of i) begins with ee then
  move i to folder ee of theLocation
                              end if
  
                    end repeat
                    set name of (the result) to text item 2 of (get name of (the result)) as string
  
          end repeat
end tell

Jan 27, 2014 3:31 PM in response to CheckYourVector

Try using:


set the_array to {}

tell application "Finder"

try

set theLocation to the selection as alias

on error

set theLocation to (folder of the front window as alias)

end try


repeat with this_file in (get files of theLocation)

set the_name to name of this_file

set the_prefix to items 1 thru ((offset of "_" in the_name) - 1) of the_name as string

if the_array does not contain the_prefix then set the_array to the_array & the_prefix

end repeat


repeat with e in the_array

if not (exists folder e of theLocation) then make new folder at theLocation with properties {name:e}

end repeat


set this_list to every file of theLocation

set AppleScript's text item delimiters to {"_"}

repeat with i in this_list

repeat with ee in the_array

try

if (name of i) begins with ee then

move i to folder ee of theLocation

set name of (the result) to text item 2 of (get name of (the result)) as string

end if

end try

end repeat

end repeat

end tell


(98842)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript - Add prefixes in file name to an array

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