Real simple objective here, just trying to create a droplet(s) that will add suffix to the end of a file’s name, that I’m dragging from the desktop.
Did browse the forum archives, but can’t quite connect the dots.
I’ve done some cobbling together of code, and get this:
on open theItem
set the PDFsuffix to "rd1"
set this_info to info for theItem
set the current_name to the name of theItem
set the new_file_name to the (the current_name & the PDFsuffix) as string
--my set_item_name(theItem, the new_file_name)
set the name of theItem to new_file_name
beep 2
--display dialog new_file_name
end open
I think, based on the error msg., I need to “do” something with the file name, like refer to it as an alias.
I’m sorry, I’ve never figured out the rules of football
Back to your script, I would do it like this
on open these_items
set the PDFsuffix to "rd1"
repeat with this_item in these_items
set {name:Nm, name extension:Ex} to (info for this_item)
if (Ex is missing value) then set Ex to ""
set Nm to {Nm, Nm's text 1 thru (-2 - (count Ex))}'s item (((Nm contains ".") as integer) + 1)
tell application "Finder" to set name of contents of this_item to (Nm & "." & PDFsuffix)
end repeat
end open
Notes: AppleScript can’t rename the file(s), the Finder should do it. on open can process multiple files and returns a list of aliases,
so a repeat block is needed
set {name:Nm, name extension:Ex} to (info for this_item)
if (Ex is missing value) then set Ex to ""
set Nm to {Nm, Nm's text 1 thru (-2 - (count Ex))}'s item (((Nm contains ".") as integer) + 1)
-- Becomes:
set Nm to name of (info for this_item)
set Ex to name extension of (info for this_item)
if Ex is missing value then set Ex to "" -- no extension
--- Now consider:
set b to "abc.d"
(b contains ".") as integer -- (true as integer) is 1
set c to "abcd"
(c contains ".") as integer -- (false as integer) is 0
set anExt to item (((Nm contains ".") as integer) + 1) --> 1 if no ".", but 2 if "." -- to be used later - not in the original.
--- End consider
--Now we define Nm as a two-part list. Part 1 is just itself, Nm.
-- item 2 of the list is "(text 1 thru (-2 - (count Ex))
-- text 1 is the first character of Nm. How much to grab, i.e. text 1 thru what?
-- answer: calculate -(2 + count Ex). if Ex was .jpeg, that would be -6, so we have:
set Nm to {Nm, text 1 thru -6 of Nm} -- where the second part is the name without the extension.
-- Now we want item anExt (from above) of that (either item 1, the original name which didn't have an extension, or item 2 with the extension clipped off, depending on whether there's a period in the name.
Since this is always going to involve PDFs, maybe I could avoid the complexity by:
-first hiding the name extension, then
-just adding “.pdf” to my little PDFsuffix text. (ie “rd1.pdf”)
Only as a joke though! It deliberately sacrifices efficiency and readability in order to get everything into one line. It’s definitely bad practice and not to be used to confuse newbies!
It appears from AutoFetishist’s latest post that the object is actually to insert a suffix between the root name and the extension.
on open these_items
set the PDFsuffix to "rd1.pdf"
repeat with this_item in these_items
set {name:Nm, name extension:Ex} to (info for this_item)
if (Ex is "pdf") then tell application "Finder" to set name of this_item to (text 1 thru -5 of Nm) & PDFsuffix
end repeat
end open
Sorry if that’s confusing, but it amounts to this: if you need to select between two options for something, you can avoid an if block as shown in the example below:
set t to "This is the first time"
-- set t to This is the second time"
if t contains "first" then
set tries to "one"
else
set tries to "many"
end if
-- Alteratively (avoiding an IF block):
set try2 to item ((("first" is in t) as integer) + 1) of {"many", "one"}
set L to {"q", "r", "s", "t"}
set I to {}
set O to {}
repeat with M in L
set N to contents of M
-- the If method
if N = "q" then
set I's end to "A"
else if N = "r" then
set I's end to "B"
else if N = "s" then
set I's end to "C"
else
set I's end to "Z"
end if
--> {"A", "B", "C", "Z"}
-- Alternatively, if you're an afficianado of one-liners, however convoluted:
set O's end to {"Z", "C", "B", "A"}'s item ((3 * ((N = "q") as integer)) + (2 * ((N = "r") as integer)) + ((N = "s") as integer) + 1)
--> {"A", "B", "C", "Z"}
end repeat