Hello Folks,
This is the standard file naming structure of my files. 3 letters, 4 numbers, 1 letter then usually 1 or 2 numbers, .ps
example… “AAA1111A1.ps” or “AAA1111A11.ps”. The last number represents a page number and should always be 2 digits.
So this script strips of the .ps, then checks to see if its a 1 or 2 digit page number. If it has only 1 number it will add a 0 to make it a double digit page number. "AAA1111A1.ps would end up “AAA111A01”.
After this is done the file is copied to 2 different folders.
My problem is that occasionally a file may get renamed but not moved. Resulting in “AAA1111A01” to stay in the original folder.
When this happens, the script will not process any new file that had the same name. It should just overwrite the file.
But the files just stay in limbo in the original folder.
Example
AAA1111A01
AAA1111A1.ps
Not sure how to get around this.
Any Ideas would be very appreciated.
property theExtension : “.ps”
property destinationFolder : “Panther HD:DoneCopy:”
property processedFolder : “Panther HD:processed:”
on adding folder items to thisFolder after receiving addedItems
delay 3
try
set theCount to the number of items in the addedItems
repeat with i from 1 to theCount
tell application "Finder"
if size of (item i of addedItems) is 0 then
delay 10
end if
end tell
delay 1
set aliasPath to (item i of addedItems)
tell application "Finder"
set theName to name of aliasPath
end tell
set theOffset to (needtoProcess(theName))
if theOffset is not 0 then
set newName to updateName(theName, theOffset)
if newName is not "" then
set theReturn to moveFile(aliasPath, newName)
display dialog "File " & newName & " processed " & theReturn giving up after 2
end if
end if
end repeat
on error errStr
end try
end adding folder items to
on needtoProcess(theName)
set theOffset to 0
set theReverse to (reverse of (characters of theExtension) as string)
if character -1 of theName is not “:” then --not a folder
set theOffset to offset of theReverse in (reverse of (characters of theName) as string)
end if
if theOffset is not 0 then
set theOffset to theOffset + 3
end if
return theOffset
end needtoProcess
on updateName(theName, theOffset)
set theReturn to “”
set theName to (text 1 thru -theOffset of theName)
set numList to “1234567890”
set charToTest to character -1 of theName
if charToTest is in numList then
if character -2 of theName is not in numList then
set theReturn to text 1 thru -2 of theName & “0” & charToTest
else
set theReturn to theName
end if
else
set theReturn to theName
end if
return theReturn
end updateName
on moveFile(theFile, newName)
set wasSuccess to 0
set folderString to theFile as string
set theFolder to getfolder(folderString)
with timeout of 1000 seconds
tell application “Finder”
activate
if not (exists item (theFolder & newName)) then
set name of theFile to newName
set fileAlias to ((theFolder & newName) as alias)
set newAlias to (duplicate fileAlias to folder destinationFolder with replacing) as alias
move fileAlias to folder processedFolder with replacing
set wasSuccess to 1
end if
end tell
end timeout
return wasSuccess
end moveFile
on getfolder(folderString)
set theOffset to offset of “:” in (reverse of (characters of folderString) as string)
return text 1 thru -(theOffset) of folderString
end getfolder
on makeSuredupisDone(newAlias)
set theCounter to 0
set doRepeat to true
repeat while doRepeat is true
try
set fileRef to open for access newAlias
close access fileRef
set doRepeat to false
on error
set theCounter to theCounter + 1
end try
end repeat
return theCounter
end makeSuredupisDone