Help please with removing the file extensions

Hi all,

I am trying to muddle my way though a little script that gets a list of PDF files from a folder and then duplicates another type of file (.job Preps File) to the amount of PDFs and renames it each time.

I have got this working but it retains the .pdf extension and removes the .job extension the script I have so far is below:

tell application "Finder"
	
	set file_name to name of (files whose name extension is in {"pdf"}) of (choose folder with prompt "Select a folder to get content file names:")
	-- Returns: {"FileName1.pdf", "FileName4.pdf", "FileName3.pdf", "FileName2.pdf"}
	
	set thePrepsFile to (choose file with prompt "Select a .job file to duplicate:")
	-- Selects which file is being duplicated
	
	set theFolder to (choose folder with prompt "Select a destination folder:")
	-- Selects where the files are going to be placed
	
	
	repeat with theItem in file_name
		set thePrepsFileCopy to duplicate thePrepsFile to theFolder
		set name of thePrepsFileCopy to (theItem as Unicode text)
	end repeat
	
end tell

Thankyou in advance for any help with this.

Andy

repeat with theItem in file_name
	set thePrepsFileCopy to duplicate thePrepsFile to theFolder
	set name of thePrepsFileCopy to (theItem as Unicode text)
	set name extension of thePrepsFileCopy to "job"
end repeat

Hi Thanks for the quick reply.

It comes up with a AppleScript Error of:
Finder got an error: Can’t set document file “PrepsPrinergy.job” of folder “Preps” of folder “Desktop” of folder “arw” of folder “Users” of startup disk to “job”.

Any ideas? I must admit I know very little about AppleScript.

Cheers
Andy

Yes, my mistake, sorry.
When the copy is renamed it becomes another file, and the last line talks to a file which doesn’t exists any more.
I’ll get back to you…

[later]
This should do it:

repeat with theItem in file_name
		set thePrepsFileCopy to duplicate thePrepsFile to theFolder
		set name of thePrepsFileCopy to (theItem as Unicode text)
		-- make a path string for the renamed file...
		set newFilePath to (theFolder as alias as text) & theItem -- this is text!
		--... and change its extension
		set name extension of alias newFilePath to "job" -- mind the 'alias'!
	end repeat

This too:

	repeat with theItem in file_name
		set thePrepsFileCopy to duplicate thePrepsFile to theFolder
		-- create a new name first:
		set AppleScript's text item delimiters to "."
		set theName to text item 1 of theItem-- drop old extension
		set AppleScript's text item delimiters to ""
		set theName to (theName as text) & ".job"-- add new one in
		-- and rename
		set name of thePrepsFileCopy to theName
	end repeat

More than one way to skin an apple :slight_smile:

Thanks :smiley:

The first one works a treat.

I have noticed that there is more than one way to do anything in AppleScript.

Cheers
Andy

@alastor933: Your second example doesn’t work on all files. If the path contains a period then you don’t have the complete files

it should be something like this

changeExtension("/Users/root/filewithnoextension", ".job") --returns :"/Users/root/filewithnoextension.job"
changeExtension("/Users/root/file.txt", ".job") --returns :"/Users/root/file.job"
changeExtension("/Users/root/com.apple.finder.plist", ".job") --returns :"/Users/root/com.apple.finder.job"

on changeExtension(posixFilePath, newExtensionName)
	set AppleScript's text item delimiters to "."
	set theTextItems to every text item of posixFilePath
	if (count theTextItems) = 1 then
		set newPosixFilePath to posixFilePath & newExtensionName as string
	else if (count theTextItems) = 2 then
		set newPosixFilePath to item 1 of theTextItems & newExtensionName as string
	else
		set newPosixFilePath to (items 1 thru -2 of theTextItems as string) & newExtensionName as string
	end if
	set AppleScript's text item delimiters to ""
	return newPosixFilePath
end changeExtension

True.
I really should learn not to dive in like that…:rolleyes:

But…
I think I’d prefer passing a file name, and leave the file specifiers in the calling script.