Monday, September 22, 2014

#1 2012-12-18 03:20:20 pm

Magyk
Member
Registered: 2008-03-07
Posts: 34

Rename file based on user input and image size

Hello everyone and happy holidays!

If someone would be so kind as to kick me in the right direction I would appreciate it!

I am trying to create a folder action script that when an image or images are dropped into the folder it will rename the file based on the image size taken from 'Image Events', the current year, and some user input.

This is the first time I have done a folder action so I am having trouble getting the script to fire off properly.  This script is loosely based on a script written by StefanK.

Thanks in advance!

Here is the script that I have so far:

Applescript:

on adding folder items to theFolder after receiving theAddedItems
   try
       repeat with thisItem in theAddedItems
           --Verify the script is starting...
           display dialog ("Let's get started") buttons "OK" default button 1
           tell application "Image Events"
               set theImg to theAddedItems
               set imgInfo to dimensions of theImg
           end tell
           set cur_year to year of (current date) as string
           set NewName to text returned of (display dialog "Please enter a new file name:" default answer "")
           tell application "Finder"
               set name of contents of thisItem to {theImg, "-", cur_year, "-", NewName}
               set thisItem to file NewName of theFolder
           end tell
       end repeat
       
   on error theMsg
       -- on error, display error message
       set myPOSIX to (POSIX path of i)
       display dialog ("File could not be renamed:" & return & return & theMsg) buttons "OK" default button 1
   end try
end adding folder items to

Offline

 

#2 2012-12-18 03:49:42 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10517
Website

Re: Rename file based on user input and image size

Magyk wrote:

This script is loosely based on a script written by StefanK.

very loosely wink

untested

Applescript:


on adding folder items to theFolder after receiving theAddedItems
   launch application "Image Events"
   try
       set cur_year to year of (current date) as string
       repeat with thisItem in theAddedItems
           --Verify the script is starting...
           display dialog ("Let's get started") buttons "OK" default button 1
           tell application "Image Events"
               set theImage to open thisItem
               set imageDimensions to dimensions of theImage
               close theImage
           end tell
           tell imageDimensions to set dimensionString to (item 1 as string) & "x" & item 2 as string
           set NewName to text returned of (display dialog "Please enter a new file name:" default answer "")
           tell application "Finder"
               set fileName to name of thisItem
               set name of contents of thisItem to (fileName & "-" & cur_year & "-" & NewName)
           end tell
       end repeat
   on error theMsg
       -- on error, display error message
       set myPOSIX to (POSIX path of thisItem)
       display dialog ("File " & myPOSIX & " could not be renamed:" & return & return & theMsg) buttons "OK" default button 1
   end try
   
end adding folder items to


regards

Stefan

Offline

 

#3 2012-12-19 10:14:40 am

Magyk
Member
Registered: 2008-03-07
Posts: 34

Re: Rename file based on user input and image size

As usual Stefan, my loosely based version was a starting point that expanded to fit my needs.  Once I have a starting point I can typically make things work the way I want.  the hard part is figuring out how to get started sometimes...roll

I modified the script a bit to allow for the user to specify the year in case they do not want to use the current year.

Can this be modified to account for multiple files added at the same time that may or may not have matching text in the original filename?

An example would be if I add three files that all have the word "Report" in the original filename but are different image sizes.  Then it would rename all of the matching files with the user input given only once (right now it asks for the new name for each item)? 
Report 1(small), Report 2(medium), and Report 3(large) would get changed to (image resolution-year-filename.jpg) with a dialog box asking for the year and filename only once.

Lastly, after the files are renamed, can we move or copy them to another folder?

Applescript:

on adding folder items to theFolder after receiving theAddedItems
   launch application "Image Events"
   try
       --display dialog ("Let's get started") buttons "OK" default button 1
       set cur_year to year of (current date) as string
       repeat with thisItem in theAddedItems
           tell application "Image Events"
               set theImage to open thisItem
               set imageDimensions to dimensions of theImage
               close theImage
           end tell
           tell imageDimensions to set dimensionString to (item 1 as string) & "x" & item 2 as string
           set NewName to text returned of (display dialog "Please enter a new file name:" default answer "")
           set imageYear to text returned of (display dialog "Use the current year?" default answer cur_year)
           tell application "Finder"
               set fileName to name of thisItem
               set name of contents of thisItem to {dimensionString & "-" & imageYear & "-" & NewName & ".jpg"}
           end tell
       end repeat
   on error theMsg
       -- on error, display error message
       set myPOSIX to (POSIX path of thisItem)
       display dialog ("File " & myPOSIX & " could not be renamed:" & return & return & theMsg) buttons "OK" default button 1
   end try
end adding folder items to

Offline

 

#4 2013-01-03 10:15:06 am

Magyk
Member
Registered: 2008-03-07
Posts: 34

Re: Rename file based on user input and image size

Stefan, did you get a chance to look at my question?

Offline

 

#5 2013-01-03 11:23:11 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10517
Website

Re: Rename file based on user input and image size

Magyk wrote:

Stefan, did you get a chance to look at my question?

Yes, but it looks quite complicated to consider all cases


regards

Stefan

Offline

 

#6 2013-01-03 11:24:59 am

Magyk
Member
Registered: 2008-03-07
Posts: 34

Re: Rename file based on user input and image size

I figured it would be rather complicated so I was just throwing that out there to see if you had encountered something similar in your travels.

How about just the last bit about moving the files that are altered?

Offline

 

#7 2013-01-03 11:29:13 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10517
Website

Re: Rename file based on user input and image size

Magyk wrote:

I
How about just the last bit about moving the files that are altered?

just add a line to move the item

Applescript:



tell application "Finder"
   set fileName to name of thisItem
   set name of contents of thisItem to (dimensionString & "-" & imageYear & "-" & NewName & ".jpg")
   move thisItem to folder "Disk:path:to:folder:"
end tell

The string path must be a full HFS path


regards

Stefan

Offline

 

#8 2013-01-03 01:39:56 pm

Magyk
Member
Registered: 2008-03-07
Posts: 34

Re: Rename file based on user input and image size

works great, thanks again Stefan.

Offline

 

Board footer

Powered by FluxBB

[ Generated in 1.269 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)