p01 Kansas City Southern
Skip to main content

Recent Topics

xx

xx
Time Machine and OS Install
by Joe
December 10, 2018, 07:18:48 PM

xx
New Windows 10 PC
by Tracy
December 10, 2018, 11:37:40 AM

poll
Poll: Work Lighting
by Tracy
December 10, 2018, 09:42:12 AM

xx
Office 365 Outlook
by frailer
December 09, 2018, 05:13:58 AM

xx
Whole of job corruption upstream?
by frailer
December 09, 2018, 05:10:44 AM

xx
Paper "scam"
by born2print
December 06, 2018, 12:23:25 PM

Topic: Re: AppleScript/Automator to create folder based on partial file name, move files (Read 1223 times) previous topic - next topic - Topic derived from Re: AppleScript/Autom...

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

OK I think this should work for you:

Code: [Select]
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!

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

Reply #1
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 10.12 Sierra | Prinergy 7.5 | Adobe Creative Cloud 2017 | Two Luscher XPose 160 CTP units

Prepress: One who does precision guess work based on unreliable data provided by those of questionable knowledge.

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

Reply #2
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.


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

Reply #3
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.

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

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

Reply #4
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


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

Reply #5
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.

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

Reply #6
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.

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

https://www.b4print.com/index.php?topic=7817.0
Mac OS 10.12 Sierra | Prinergy 7.5 | Adobe Creative Cloud 2017 | Two Luscher XPose 160 CTP units

Prepress: One who does precision guess work based on unreliable data provided by those of questionable knowledge.

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

Reply #7
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.

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!

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

Reply #8
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?

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

Reply #9
/Applications/Utilities/Script Editor

Open it and create a new script and paste in:

Code: [Select]
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 10.12 Sierra | Prinergy 7.5 | Adobe Creative Cloud 2017 | Two Luscher XPose 160 CTP units

Prepress: One who does precision guess work based on unreliable data provided by those of questionable knowledge.

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

Reply #10
Hey Cool! So where do you run these from?

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

Reply #11
If you save it as a script or an application you just double click it to run it.
Mac OS 10.12 Sierra | Prinergy 7.5 | Adobe Creative Cloud 2017 | Two Luscher XPose 160 CTP units

Prepress: One who does precision guess work based on unreliable data provided by those of questionable knowledge.

 

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

Reply #12
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:

Code: [Select]
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.

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

Reply #13
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)

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

Reply #14
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.

Code: [Select]
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.