Re: AppleScript/Automator to create folder based on partial file name, move files

Started by davidkyoo, April 10, 2018, 03:04:49 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

davidkyoo

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!

Joe

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.
Mac OS Sonoma 14.2.1 (c) | (retired)

The seven ages of man: spills, drills, thrills, bills, ills, pills and wills.

swampymarsh

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.


davidkyoo

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

swampymarsh

Glad you got it sorted, I may as well post my alternative using 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


Tracy

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.

Joe

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
Mac OS Sonoma 14.2.1 (c) | (retired)

The seven ages of man: spills, drills, thrills, bills, ills, pills and wills.

swampymarsh

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!

Tracy

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?

Joe

/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.
Mac OS Sonoma 14.2.1 (c) | (retired)

The seven ages of man: spills, drills, thrills, bills, ills, pills and wills.

Tracy


Joe

If you save it as a script or an application you just double click it to run it.
Mac OS Sonoma 14.2.1 (c) | (retired)

The seven ages of man: spills, drills, thrills, bills, ills, pills and wills.

swampymarsh

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

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.

Tracy

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)

Fat Boy Tim

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.