I’m new to scripting but I have an application that won’t let me do exactly what I need so am hoping a script would be the answer. The app I have will split PDFs into individual pages and give a prefix of the page number, always starting at 1. But the PDFs are in sections with the file name stipulating the page number of the first page. So for example the original document could be called jobname_p052.pdf and contain four pages. The individual files are then called jobname_p052+1.pdf, jobname_p052+2.pdf, jobname_p052+3.pdf & jobname_p052+4.pdf. So I need a script that will take everything after the _p and, add the total together and subtract 1, giving the real numbers. So that I then have jobname_p052.pdf, jobname_p053.pdf, jobname_p054.pdf & jobname_p055.pdf. Any ideas?
set original_path to (path to desktop as text) & "jobname_p052+4.pdf"
set new_path to my renommeWithNumber(original_path)
on renommeWithNumber(original_path)
tell application "System Events" to tell disk item original_path
set orig_name to name
set source_folder to path of container
end tell
set bare_name to my recolle(items 1 thru -2 of my decoupe(orig_name, "."), ".")
set splitted_name to my decoupe(bare_name, "_p")
set bare_name to my recolle(items 1 thru -2 of splitted_name, "_p")
set {num1, num2} to my decoupe(item -1 of splitted_name, "+")
set new_name to bare_name & "_p" & text -3 thru -1 of ("000" & (num1 + num2)) & ".pdf"
tell application "System Events" to set name of disk item original_path to new_name
return source_folder & new_name
end renommeWithNumber
--=====
on decoupe(t, d)
local oTIDs, l
set oTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to d
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
--=====
on recolle(l, d)
local oTIDs, t
set oTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to d
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
--=====
may help.
Yvan KOENIG (VALLAURIS, France) mardi 7 juin 2011 11:07:39
set my_folder to ((choose folder) as text)
tell application "System Events"
set the_paths to path of every disk item of folder my_folder whose type identifier is "com.adobe.pdf"
repeat with a_path in the_paths
if a_path contains "_p" and a_path contains "+" then
set renamed to my renommeWithNumber(a_path)
end if
end repeat
end tell
on renommeWithNumber(original_path)
tell application "System Events" to tell disk item original_path
set orig_name to name
set source_folder to path of container
end tell
set bare_name to my recolle(items 1 thru -2 of my decoupe(orig_name, "."), ".")
set splitted_name to my decoupe(bare_name, "_p")
set bare_name to my recolle(items 1 thru -2 of splitted_name, "_p")
set {num1, num2} to my decoupe(item -1 of splitted_name, "+")
set new_name to bare_name & "_p" & text -3 thru -1 of ("000" & (num1 + num2)) & ".pdf"
tell application "System Events" to set name of disk item original_path to new_name
return source_folder & new_name
end renommeWithNumber
--=====
on decoupe(t, d)
local oTIDs, l
set oTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to d
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
--=====
on recolle(l, d)
local oTIDs, t
set oTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to d
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
--=====
As you may see, I dislike telling to Finder
Yvan KOENIG (VALLAURIS, France) mardi 7 juin 2011 22:58:19
Thanks again Yvan, I hope this is helpful to the community and not just myself!
What I actually meant though, was how do I set a folder (e.g. called ‘In’) so that whenever a file lands in it the name is converted and then preferentially gets passed to another (‘out’) folder? There’ll be several different titles, each with varying sections arriving all the time and I need them converted as quickly as possible.
Thanks again for your help, and my apologies for not being clearer before!!
Here is the same script designed as a Folder Action one.
(*
my maybe("Macintosh HD:Users:yvankoenig:Desktop:dossier sans titre:", {"Macintosh HD:Users:yvankoenig:Desktop:dossier sans titre:Guide_AppleScript_p60+9.pdf"})
on maybe(this_folder, added_items)
*)
on adding folder items to this_folder after receiving added_items
tell application "System Events"
if not (exists folder "Renamed" of folder (this_folder as text)) then make new folder at end of folder (this_folder as text) with properties {name:"Renamed"}
end tell
set destination_unix to quoted form of POSIX path of ("" & this_folder & "Renamed")
repeat with a_path in added_items
set a_path to a_path as text
tell application "System Events"
set maybe to a_path contains "_p" and a_path contains "+" and type identifier of disk item a_path is "com.adobe.pdf"
end tell
if maybe then
tell application "System Events" to set old_name to name of disk item a_path
do shell script "mv " & (quoted form of POSIX path of a_path) & space & destination_unix
set renamed to my renommeWithNumber("" & this_folder & "Renamed:" & old_name)
end if
end repeat
end adding folder items to
(*
end maybe
*)
on renommeWithNumber(original_path)
tell application "System Events" to tell disk item original_path
set orig_name to name
set source_folder to path of container
end tell
set bare_name to my recolle(items 1 thru -2 of my decoupe(orig_name, "."), ".")
set splitted_name to my decoupe(bare_name, "_p")
set bare_name to my recolle(items 1 thru -2 of splitted_name, "_p")
set {num1, num2} to my decoupe(item -1 of splitted_name, "+")
set new_name to bare_name & "_p" & text -3 thru -1 of ("000" & (num1 + num2)) & ".pdf"
tell application "System Events" to set name of disk item original_path to new_name
return source_folder & new_name
end renommeWithNumber
--=====
on decoupe(t, d)
local oTIDs, l
set oTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to d
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
--=====
on recolle(l, d)
local oTIDs, t
set oTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to d
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
--=====
Yvan KOENIG (VALLAURIS, France) mercredi 8 juin 2011 14:14:49
Hi Yvan,
I’m probably driving you mad with this and I’ve tried to figure it out myself but what the hell do I do to get this working. I got your scripts working no problem but when I assign a folder action in Automator to use this script it does nothing
Folder actions scripts aren’t related to Automator (the truth is that I never use Automator).
A folder actions script like the one which I posted in my late message must be installed in a dedicated folder.
So the exact path to the posted one is :
Macintosh HD:Library:Scripts:Folder Action Scripts:rename_some_PDFs.scpt
When this is done, you must attach the script to the folder to which it’s supposed to apply.
To attach a script, you must use :
Macintosh HD:Library:Scripts:Folder Actions:Folder Actions Setup
It’s an alias pointing to an application belonging to the operating system.
When the script is attached to the folder, it’s automatically triggered when a file (or several ones) is added to the folder.
English isn’t my language so I’m not sure that my explanations are really clear.
Sometimes, the treatment of folder actions is slow so it may be useful to edit the script a bit.
No longer treat the added_items but build a list of the PDF stored in the folder.
This way, if one of them was ‘forgotten’ it will be treated this time.
Yvan KOENIG (VALLAURIS, France) lundi 13 juin 2011 19:22:08