Detect and change final part of file's name

Hi.
I’ve made a simply application for detect and convert some graphic file. The script works well.
Now I need that the script detect if the name of file end with “_1” or “_2” or “_3” or “_4” (before extension) and, if yes, proceed, if not, insert the postfix “_1” and then proceed.

This is much complicated, I know: I am able to rename a file or to change add the postfix, but not to detect if the file has the right postfix or not.

Is that a job that AppleScript can do?

Something like this (there are many ways to do this knowing the extension of the file, but I don’t know what the rest of the script “knows”).


set myNames to {"Pic.jpg", "aPic_1.tif", "bPic_3.png"}
repeat with aName in myNames
	if (offset of "_" in (contents of aName)) is 0 then
		set O to offset of "." in (contents of aName) -- assuming only one period
		set contents of aName to text 1 thru (O - 1) of aName & "_1" & text O thru -1 of contents of aName
	end if
end repeat
myNames --> {"Pic_1.jpg", "aPic_1.tif", "bPic_3.png"}

Uhm.
i didn’t know the offset command. It appears lot intresting, but also lot strange.
i have to study more exemples for undestand what you write, sorry, but I’m learning AS from not so many days.

Tnx a lot

Offset is the number of characters counted from the beginning of a string to the first character in the test string (which can be more than one character). It returns 0 if the test string is not found.


set STR to "Now is (a stray flight) the time for all good men"
set Ofor to offset of "for" in STR
--> 34 characters to the "f" in "for", but flight is ignored.
--> ("foresome" would not be ignored - it would count).
set Ox to offset of "x" in STR --> 0

Another (more reliable) way to get the base name of a file (without the extension) is

set f to (choose file) -- for illustration
tell (info for f) to set {Nm, Ex} to {name, name extension}
set BN to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
BN

so you could change the basename and then add the extension back on with a period before it.

I don’t think I’ve ever seen that one, Adam; It’s works nicely as a one-line handler:

on nameWithoutExtension for someAlias
	-- `someAlias` should be an alias or file specification
	tell (info for someAlias) to return text 1 thru ((my (offset of ("." & name extension) in name)) - 1) of name
end nameWithoutExtension

choose file
nameWithoutExtension for result

That’s the way I use it myself, Bruce. Glad you liked it - I call it BaseName(FileRef). :cool:

Just thought of something Adam. As is, this won’t work properly on a name such as “boom.txt.txt” (which, will odd, is a legitimate name). I think you can fix this by adding in a couple of colons (the only character which can’t be used in filenames with HFS+).

on nameWithoutExtension for someAlias
	-- `someAlias` should be an alias or file specification
	tell (info for someAlias) to return text 1 thru ((my (offset of ("." & name extension & ":") in (name & ":"))) - 1) of name
end nameWithoutExtension

choose file
nameWithoutExtension for result

Hi Bruce,

I use this

on nameWithoutExtension(someAlias)
	tell (info for someAlias) to set {Nm, Ex} to {name, name extension}
	if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
	return {Nm, Ex}
end nameWithoutExtension

set {fName, fExt} to nameWithoutExtension(choose file)

Hi, Stefan.

On both my machines, ‘info for’ returns ‘missing value’ when there’s no name extension, which causes your middle line to error. This seems to be safer, although it assumes that only visible files will be chosen:

on nameAndExtension(someAlias)
	set Nm to name of (info for someAlias)
	if (Nm contains ".") then
		set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "."
		set {Nm, Ex} to {text 1 thru text item -2, text item -1} of Nm
		set AppleScript's text item delimiters to astid
		set Ex to "." & Ex -- Omit if not required.
	else
		set Ex to ""
	end if
	return {Nm, Ex}
end nameAndExtension

set {fName, fExt} to nameAndExtension(choose file)

The optional line can show up a name that ends with a dot, which is rare but possible.

O.K., whats about that? :wink:

on nameWithoutExtension(someAlias)
	tell (info for someAlias) to set {Nm, Ex} to {name, name extension}
	if Ex is not "" and Ex is not missing value then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
	return {Nm, Ex}
end nameWithoutExtension

set {fName, fExt} to nameWithoutExtension(choose file without invisibles)
-- Panther or later.
on nameAndExtension(someAlias)
	set {name:Nm, name extension:Ex} to (info for someAlias)
	if (Ex is missing value) then set Ex to ""
	return {{Nm, Nm's text 1 thru (-2 - (count Ex))}'s item (((Nm contains ".") as integer) + 1), Ex}
end nameAndExtension

set {fName, fExt} to nameAndExtension(choose file without invisibles)

:stuck_out_tongue: