Batch & Rename sequentially

I have a lot of JPGs on the /tmp directory that i have printed from the web using Kunvert.app via “PDF Services” but every time i print one it is saved on a new folder named “printing(dot sumtin)” and the files are been saved as “Print job.jpg” now, i have to take those files out so i have made a contextual menu using “on my command” to move the files from /tmp to my desktop but then all files are overwritten by the last one and i end with just one file…


  #On my command script (Move JPGs) 
############### 
 mv /tmp/printing*/* /Users/myhomedirectory/Desktop/ 
 ##############
 

I’m interested on doing a script to move and rename those files sequentially to my Desktop or any other directory. I have tried to make an applescript but then the script is unable to see the /tmp directory.

I had the same problem months ago when a client gave me a CD with lots of folders with some files inside but all the files was named the same way no matter in which folder they were stored… the procedure then was to use “R-name.app” to rename those files from something.pdf to something ‘00+1’.pdf that doesn’t bothered me since i just have to drop the folders from CD to app but now i have to:

Print to JPG

  • go to /tmp
  • select folders
  • drop to R-name.app
  • run contextual menu script in order to move all files to a directory

By now I’m not even halfway doing this job and I still have a lot of files to print this way so I have made other script but I’m sure it can be improved someway since it opens Kunvert.app and R-Name.app to do the job…

#Move&Rename after Printing
 #All PDFs have been converted to JPGs during the print process via Kunvert and PDF Services 
 #This script will make a new dir on the Desktop then it will open R-Name in order to add a prefix/suffix to rename files sequentially and pauses the process till R-Name is closed then the process will go ahead and will move all file to the newly created folder once the files are there, all the dirs in /tmp named "printing" will be deleted 
  
 #!/bin/sh 
 cd ; mkdir /Users/MyHomeDir/Desktop/imgs ; cd /Users/MyHomeDir/Desktop/imgs 
 open -a R-Name /tmp/printing*/*  
 mv /tmp/printing*/* /Users/MyHomeDir/Desktop/imgs/ 
 rm -R /tmp/printing*

I have found on this forums a way to rename files in a single process but I still have two problems 1) the script can’t see files inside subfolders 2) it seems that the script needs root privileges to browse the /tmp directory. The only way for me to get to that directory using applescript is by an alias of /tmp placed in a more accessible path, then again it can’t browse subfolders

BTW I’ve chaged somethings to the original scrip made by… (damn i forgot your name and i don’t remember how i get to that thread… thx a lot anyway) like the prefix line and now the script will rename only JPEG files.

property the_prefix : "0"

tell application "Finder"
	open file "tmp" in folder "X" in folder "XXX" in disk "XXX" -- this is the alias of /tmp
end tell

try
	tell application "Finder" to set the source_folder to (folder of the front window) as alias
on error -- no open folder windows 
	set the source_folder to path to desktop folder as alias
end try

tell application "Finder"
	-- get the current folder listing 
	set sourceFiles to every file of source_folder whose name contains ".jpg"
	-- start off with file 1 
	set fileCounter to 1
	--loop through the files 
	repeat with aFile in sourceFiles
		--renaming each one in turn 
		set name of aFile to (the_prefix & fileCounter & ".jpg") as string
		-- and incrementing the counter after each file 
		set fileCounter to fileCounter + 1
	end repeat
end tell

The last part of this script will be:

do shell script "#!/bin/sh ; mv /tmp/printing*/* /Users/MyHomeDir/Desktop/imgs/  ;
 rm -R /tmp/printing*"

to move the renamed files the desktop and then delete all folders named “printing” on the /tmp directory.

What do you think is it possible to do what I’m trying to do?

TIA

Now i can get a list of files but can’t rename them…

property the_prefix : "add "

tell application "Finder"
	open folder "X" in folder "XXXl" in disk "XXX"
end tell

try
	tell application "Finder" to set the source_folder to (folder of the front window) as alias
on error -- no open folder windows 
	set the source_folder to path to desktop folder as alias
end try
tell application "Finder" to set FolderList to get the name of every folder of source_folder
repeat with thisFolder in FolderList
	set thisSubFolder to (source_folder as string) & thisFolder as string
	tell application "Finder" to set filelist to (get the name of every file of folder thisSubFolder whose name contains "jpg") as list
	repeat with thisFileName in filelist
		set thisPath to (thisSubFolder as string) & ":" & thisFileName as string
		tell application "Finder"
			-- get the current folder listing  
			set sourceFiles to every file of filelist
			-- start off with file 1 
			set fileCounter to "1" as number
			--loop through the files 
			repeat with aFile in filelist
				--renaming each one in turn 
				set name of aFile to (the_prefix & fileCounter & ".jpg") as string
				-- and incrementing the counter after each file 
				set fileCounter to fileCounter + 1
			end repeat
		end tell
	end repeat
end repeat
Can't set name of "whatever.jpg" to "add 1.jpg"

any ideas?

Hi :slight_smile:
Here one small suggestion of recursivity handler:

property MySuffixe : "add "

on run
	my SubRename(choose folder)
end run

on SubRename(MyFolder)
	tell application "Finder"
		activate
		set MyList to every item of MyFolder
		repeat with Loop from 1 to (count every item in MyList)
			set MyItem to item Loop of MyList
			set MyType to file type of MyItem
			if (MyType is "fold") then
				my SubRename(MyItem)
			else if (MyType is "jpeg") then
				set name of MyItem to (MySuffixe & Loop & ".jpg")
			end if
		end repeat
	end tell
end SubRename

:wink:

Hi Fredo d;o) and thanks your script works great for renaming files and it’s cleaner than mine although it seems to have some problems with subfolders and items inside those

 FInder got an error: Can't get file type of folder "untitled folder" of folder "XX" of disk "X"

Both scripts yours and mine seems to have some issues with subfolders but also both are working fine renaming files, nevertheless i can’t understand the error returned by yours… in my script the answer was very clear saying it can’t rename the files into subfolders i guess it has something to do with the path… in yours the error is that it can’t get the file type but it also knows that it is a folder… if it can’t get the type how does it knows it’s a folder?

I’m new to this scripting thingy and I’m having fun doing it… :smiley:

thx again…

Ok… its strange…
And if we modify this instruction in the loop:

set MyItem to item Loop of MyList 

… by this other syntax:

set MyItem to (item Loop of MyList) as alias

that works well now?

Ouch!! same error, on the same items:

FInder got an error: Can't get file type of alias "XX:X:Untitled folder"

it seems that the script only have issues with folders, what about telling the script to bypass “unknown” file types and continue or, if it’s possible, something like -On Error “blah” then continue"-

Also I’ve added -whose name contains “jpg”- to some lines of the script getting errors from -Can’t get file “blah”- to -Access not allowed-

Fredo d;o) you rock thx to your script :smiley:

this is how it looks now

property MySuffixe : " add "

on run
	my SubRename(choose folder)
end run

on SubRename(MyFolder)
	tell application "Finder"
		activate
		set MyList to every item of MyFolder
		repeat with Loop from 1 to (count every item in MyList)
			set MyItem to (item Loop of MyList) as alias
			set MyType to get the name of every item of MyItem
			set FolderName to get the name of MyFolder
			if (the name of MyItem contains "folder") then
				my SubRename(MyItem)
			else if (the name of MyItem contains "jpg") then
				set name of MyItem to (FolderName & MySuffixe & Loop & ".jpg")
			end if
		end repeat
	end tell
end SubRename

Now it’s time to move the file to the a new folder but that’s the easy part… :stuck_out_tongue:

THANKS AGAIN!!! :smiley:

Hi BoneFill :slight_smile:
Here another suggestion with move files to a same folder
(and with the sequential number for the new name of files) :


property Nro : 0
property MySuffixe : "add "
property MyDestinationFolder : ""

on run
	set Nro to text returned of (display dialog "Start with number :" ¬
default answer Nro with icon 1) as number
	set MyFilesFolder to ¬
		(choose folder with prompt "Choose the files folder :")
	set MyDestinationFolder to ¬
		(choose folder with prompt "Choose the destination folder :")
	my SubRename(MyFilesFolder)
end run

on SubRename(MyFolder)
	tell application "Finder"
		activate
		set MyList to every item of MyFolder
		repeat with Loop from 1 to (count every item in MyList)
			set MyItem to (item Loop of MyList) as alias
			if (last character of ("" & MyItem) is in {":", "/"}) then
				my SubRename(MyItem)
			else if (end of MyItem is ".jpg") then
				set Nro to Nro + 1
				set name of MyItem to ¬
					(MySuffixe & (text -4 thru -1 of ("000" & Nro)) & ".jpg")
			end if
		end repeat
		move every item of MyFolder to MyDestinationFolder
	end tell
end SubRename

:wink:

Ho… sorry, the last script are the several bugs… :frowning: :?
Here the new code (tested with Os9):

property Nro : 1
property MySuffixe : "add "
property MyDestinationFolder : ""

on run
	set Nro to text returned of (display dialog ¬
		"Start with number :" default answer Nro with icon 1) as number
	set MyFilesFolder to ¬
		(choose folder with prompt "Choose the files folder :")
	set MyDestinationFolder to ¬
		(choose folder with prompt "Choose the destination folder :")
	my SubRename(MyFilesFolder)
end run

on SubRename(MyFolder)
	tell application "Finder"
		activate
		set MyList to every item of MyFolder
		repeat with Loop from 1 to (count every item in MyList)
			set MyItem to (item Loop of MyList) as alias
			if (last character of ("" & MyItem) is in {":", "/"}) then
				my SubRename(MyItem)
			else if (("" & MyItem) contains ".jpg") then
				set name of MyItem to ¬
					(MySuffixe & (text -4 thru -1 of ("000" & Nro)) & ".jpg")
				move MyItem to MyDestinationFolder
				set Nro to Nro + 1
			end if
		end repeat
	end tell
end SubRename

:wink:

WOOHOO!!! :smiley:

That works great… the naming scheme is way better than having the folder name in the file name… also it’s cool to have the option for the count number to start from…

This is how i was moving files after renaming which can work similar to yours but i made it to move files from a specific location and then delete the folders where the moved items were into…

property MySuffixe : " img 0"

on run
	my SubRename(choose folder)
	my MovetoFolder(choose folder)
end run

on SubRename(MyFolder)
	tell application "Finder"
		activate
		set MyList to every item of MyFolder
		repeat with Loop from 1 to (count every item in MyList)
			set MyItem to (item Loop of MyList) as alias
			set MyType to get the name of every item of MyItem
			set FolderName to get the name of MyFolder
			if (the name of MyItem contains "printing") then
				my SubRename(MyItem)
			else if (the name of MyItem contains "jpg") then
				set name of MyItem to (FolderName & MySuffixe & Loop & ".jpg")
			end if
		end repeat
	end tell
end SubRename

on MovetoFolder(NextFolder)
	set ItemsGoHere to POSIX path of NextFolder
	do shell script "mv /tmp/printing*/*" & " " & ItemsGoHere
	do shell script "rm -R /tmp/printing*"
end MovetoFolder

I like your new script it works faster and better… :smiley: