B4Print.com

Operating Systems => Macintosh => Topic started by: davidkyoo on April 10, 2018, 03:04:49 PM

Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: davidkyoo on April 10, 2018, 03:04:49 PM
Quote from: Joe on July 10, 2015, 09:45:15 PMOK I think this should work for you:

set chosenFolder to (choose folder)
tell application "Finder" to set fileList to files of chosenFolder

repeat with aFile in fileList
set {name:Nm, name extension:Ex} to info for (aFile as alias)
if Ex is missing value then set Ex to ""
if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
set dateFolder to text 1 thru 5 of Nm
set destinationFolder to quoted form of (POSIX path of chosenFolder & dateFolder)
do shell script "mkdir -p " & destinationFolder
end repeat

Hi Joe,

I found this thread but was wondering if there could be a tweak to this script.

I have multiple file names that aren't always restricted to 1 through 5 numbers, just like below:

CK117T_BLACK_000
SY241T_ARECALEAF_003
SY231T_OFFWHITE_002
SY217T_PINK_003

I would like to make folders of the file names without the numbers but including the colors as well and have all the file name that is associated with the style number and color go inside the newly created folder:

ex)
CK117T_BLACK
SY241T_ARECALEAF
SY231T_OFFWHITE
SY217T_PINK

The initial script from the thread allowed only 1 through 5 text to be created as folder, so the folder would show as:

ex)
CK117
SY241
SY231
SY217

Would there be a way to set a parameter to create folders including the color but removing the "_00X" ? (CK117T_BLACK_000)

Thanks in advance!
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: Joe on April 10, 2018, 05:45:18 PM
I'll have to go back and re-read the original thread as I have long forgot about what we were even doing there. :D

I'll back to you as soon as I get some time to look at it again.
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: swampymarsh on April 11, 2018, 07:24:34 AM
I can't AppleScript, but I can potentially offer a solution.

Starting with something like the attached image "orig.png"... and ending up with the attached image "filed.png"

Is that what you would like to do?

If so, you would need to install a free 3rd party program.

Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: davidkyoo on April 11, 2018, 09:01:26 AM
Quote from: swampymarsh on April 11, 2018, 07:24:34 AMI can't AppleScript, but I can potentially offer a solution.

Starting with something like the attached image "orig.png"... and ending up with the attached image "filed.png"

Is that what you would like to do?

If so, you would need to install a free 3rd party program.

I actually did find a solution.

Thanks!

set chosenFolder to (choose folder)
tell application "Finder" to set fileList to files of chosenFolder

repeat with aFile in fileList
   set {name:Nm, name extension:Ex} to info for (aFile as alias)
   
   set strLength to the length of Nm
   
   if Ex is missing value then set Ex to ""
   if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
   set dateFolder to text 1 thru ((count strLength) - 5) of Nm
   set sourceFile to quoted form of POSIX path of (aFile as text)
   set destinationFile to quoted form of (POSIX path of chosenFolder & dateFolder & "/" & name of aFile)
   do shell script "ditto " & sourceFile & space & destinationFile
   do shell script "rm " & sourceFile
end repeat
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: swampymarsh on April 11, 2018, 05:05:38 PM
Glad you got it sorted, I may as well post my alternative using ExifTool (https://www.sno.phy.queensu.ca/~phil/exiftool/):

The ExifTool command line code:

exiftool '-directory<${directory}/${filename;s#(^.+)(_.+)#$1#}' -r '/Users/currentuser/Desktop/Input Folder'

If you know regular expressions used in Adobe Bridge Batch Rename or "grep" as used in InDesign, then you will recognise the capture group find/replace code syntax highlighted in blue and green.

All you need to do to use this in Apple Automator is to change the code to the following:

/usr/local/bin/exiftool '-directory<${directory}/${filename;s#(^.+)(_.+)#$1#}' -r "$@"

The absolute path to the top level folder is replaced with a shell script variable: "$@"

Then put it into a Run Shell Script action using /bin/bash passed as arguments.

I like to use the Automator "Service" option that receives files/folders from the Finder, so that the command is available from a right click.

Ensure that any copy/paste code does not convert straight single or double quotes to "curly" quotes.

A screenshot of the Automator setup is attached, with a few other examples at this link:

http://prepression.blogspot.com.au/2016/12/automator-diy-exiftool-gui-services.html

Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: Tracy on April 12, 2018, 09:05:25 AM
This is all greek to me but something I would like to learn
If anyone has a link to the basics of this, that would be great.
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: Joe on April 12, 2018, 09:09:37 AM
Quote from: Tracy on April 12, 2018, 09:05:25 AMThis is all greek to me but something I would like to learn
If anyone has a link to the basics of this, that would be great.

Back to the original thread is a good place to start:

https://www.b4print.com/index.php?topic=7817.0
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: swampymarsh on April 12, 2018, 05:39:54 PM
Quote from: Tracy on April 12, 2018, 09:05:25 AMThis is all greek to me but something I would like to learn
If anyone has a link to the basics of this, that would be great.

Which bits are of interest Tracy?

Learning AppleScript? AppleScript is often touted as an "easy" scripting language to learn (compared to say cross platform JavaScript or Windows OS VBScript) as it is closer to "plain english" – however the term "easy" is relative when it comes to scripting languages.

Or Apple Automator (like AppleScript, this is Mac OS only)? Although this is mostly drag-n-drop GUI stuff, one can add shell script code, AppleScript code etc.

ExifTool (cross platform) command line code?

Regular Expressions/GREP (cross platform) as found in Adobe Bridge Batch Rename and Adobe InDesign?

The world is your oyster!
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: Tracy on April 13, 2018, 03:25:32 PM
Just to write an Apple script, I went to the original Thread.
So I guess I need to start with, go ahead and laugh, where is the Apple Script Editor :laugh:
So What would be something super simple you can do with it?
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: Joe on April 13, 2018, 03:46:25 PM
/Applications/Utilities/Script Editor

Open it and create a new script and paste in:

display dialog "Hello, world"
Then hit the little triangle 'Play' button. You have created your first application. Save As an application if you want.
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: Tracy on April 13, 2018, 04:08:21 PM
Hey Cool! So where do you run these from?
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: Joe on April 13, 2018, 04:55:35 PM
If you save it as a script or an application you just double click it to run it.
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: swampymarsh on April 15, 2018, 07:28:50 PM
Another of many ways to run this is to attach the script to a folder, so that whenever a new file is added to the folder the script will be run (hot folder).

The code would need minor modification:

on adding folder items to this_folder after receiving added_items
display dialog "Hello, world"
end adding folder items to

Then it would need to be linked up to a folder:

https://www.macworld.com/article/1167157/software-utilities/automate-tasks-with-folder-actions.html (https://www.macworld.com/article/1167157/software-utilities/automate-tasks-with-folder-actions.html)

Apple Automator can also create a folder action and also accept AppleScript code (screenshot attached). Automator simplifies the folder action setup compared to the pure AppleScript route.
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: Tracy on April 16, 2018, 08:25:58 AM
I see how this can be very useful, I hope I get some time to play around with it soon.
Thanks guys!
Got any examples of what you use it for? (simple ones)
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: Fat Boy Tim on April 16, 2018, 11:00:10 AM
This is one I use a lot.

Place File in Folder of Same Name

I have it saved as. a Service with Automator so I can right click on any file and it put into a folder with the same name in the same folder you started with.

on run {input, parameters}

(* Your script goes here *)


tell application "System Events"

--- get the details of the first item

set myPOSIXitem to the POSIX path of (first item of input)

set myItemAlias to the first item of input as alias

-- build the name for the new folder

set myItemNameExtension to the name extension of myItemAlias

set myItemNameExtensionLength to the length of myItemNameExtension

if myItemNameExtensionLength = 0 then

set myItemNameExtensionLength to -1 --- in case the target is a folder

end if

set myPOSIXitemLength to (length of myPOSIXitem) - (myItemNameExtensionLength + 1)

set myNewFolderName to the quoted form of ((characters 1 thru myPOSIXitemLength of myPOSIXitem) as string)

-- make the folder

do shell script "mkdir -p " & myNewFolderName

-- copy the first item to the new folder

set myPOSIXitem to the quoted form of myPOSIXitem

do shell script "mv " & myPOSIXitem & " " & myNewFolderName

end tell

return input
end run


But - I would also recommend HAZEL from NoodleSoft.

https://www.noodlesoft.com

It's very cheap and can be used to move, rename, copy & sync all kinds of files and folders.
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: Tracy on April 16, 2018, 11:27:29 AM
That sounds good, does this script need adjusting?
I see it says "your script goes here"
also I don't know how to save it as a Service with Automater
can you give me the steps on how to use this? very interesting.
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: swampymarsh on April 16, 2018, 03:29:50 PM
Tracy, when creating a new Automator doc, you can select "Service" or another option.

Screenshot attached.

I'd advise you to Google up and search for Automator and or AppleScript sites, there are many websites out there with great info to get beginners past the initial hurdles of getting started.
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: Tracy on April 16, 2018, 03:38:57 PM
Hey thanks Swampy!
That should help
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: swampymarsh on April 16, 2018, 03:43:19 PM
Quote from: Tracy on April 16, 2018, 03:38:57 PMHey thanks Swampy!
That should help

After creating your new blank service in Automator, copy/paste in the code and hit the "compile" spanner icon as attached.

To run, contextual/control/right click on a file and select your service command.
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: swampymarsh on April 16, 2018, 03:46:20 PM
Quote from: Tracy on April 16, 2018, 11:27:29 AMThat sounds good, does this script need adjusting?
I see it says "your script goes here"
also I don't know how to save it as a Service with Automater
can you give me the steps on how to use this? very interesting.

No adjustment required, the bit that is marked as:

(* Your script goes here *)

Is what is known as a "comment" which is ignored by the scripting engine and is used to document the script.

That line can be left, or removed.

In an earlier post I showed AppleScript code in two separate attachments, one "complex" and one "simplified".
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: swampymarsh on April 16, 2018, 04:10:21 PM
As previously mentioned, I personally find ExifTool a whole lot easier to use and it is easy enough to put it into an Automator service (http://prepression.blogspot.com.au/2016/12/automator-diy-exiftool-gui-services.html) for easy right click access.

The AppleScript code posted by Fat Boy Tim used 43 lines of code and 1051 characters.

The equivalent ExifTool command only uses 1 line of code and 91 characters and is cross platorm (apart from the specific shell script bits that are Mac OS only).

Both methods process "instantly" and will create a new folder based on the filename of the selected file and move the file into the newly created folder. The ExifTool command has the advantage of working on multiple selected files. Of course, ExifTool can "only do so much" and it is not a scripting language, so it is a case of horses for courses.

/usr/local/bin/exiftool '-directory<${directory}/${filename;s/(^.+)(\.[^.]*$)/$1/}' -r "$@"
Title: Re: AppleScript/Automator to create folder based on partial file name, move files
Post by: Tracy on April 17, 2018, 08:20:42 AM
This is excellent stuff, I will practice it an read about it.
I hope I get it!
wish you guys were here  :laugh: