Change name of file and then open the file

In the script I have here, I’d like to rename the file, and then once the file is renamed I’d like to open it. How can I do this?


on open
set olddelims to AppleScript's text item delimiters -- save their current state
   tell application "Finder" to set fileList to the selection
   set searchStrings to {"Aunt", "Uncle", "Grandmother"}
   set replaceStrings to {"Pam", "Bill", "Jeanette"}
   repeat with aFile in fileList
       set fileName to name of aFile
       repeat with i from 1 to count of searchStrings
           set AppleScript's text item delimiters to item i of searchStrings
           set newName to every text item of fileName
           set AppleScript's text item delimiters to item i of replaceStrings
           set fileName to newName as string
       end repeat
       set name of aFile to fileName
   end repeat
   set AppleScript's text item delimiters to olddelims--return to old state
end open

I’ve tried this:


	set name of aFile to current_Filename as string
		tell application "Finder" to set file_name to (name of current_Filename)
		set thePath to current_Filename as text
		set theposix to POSIX path of thePath
		display dialog thePath
		do shell script "open " & the quoted form of theposix

and it tells me that it can’t get the name of the file.

I’ve tried this:


	set name of aFile to current_Filename as string
		tell application "Finder" to set file_name to (name of aFile)
		set thePath to aFile as text
		set theposix to POSIX path of thePath
		display dialog thePath
		do shell script "open " & the quoted form of theposix

and it just returns an error message…something along the lines of “can’t get <>” etc…

How can I tell Applescript to open the file that it just renamed?

I’ve had to do the same thing with a script I made to move downloaded wallpaper. If a wallpaper with the same name already exists, I have it rename it, then move it. Look towards the bottom of the script to see the solution.

-- sort wallpapers
tell application "Finder"
	try
		set DLfolder to folder (path to downloads folder as text)
		set originalWP to files of DLfolder whose name extension is "jpg"
		set moveWP to move originalWP to folder ((path to pictures folder as text) & "Wallpaper:")
		-- if wallpaper exists
	on error number -15267
		repeat with eachWP in originalWP
			set originalName to name of eachWP
			set nameOffset to offset of "." in originalName
			set justName to characters beginning through (nameOffset - 1) of originalName as text
			set newName to (justName & " II" & ".jpg")
			set name of eachWP to (newName as text)
			set newWP to ((path to downloads folder as text) & newName)
			set finalWP to move newWP to folder ((path to pictures folder as text) & "Wallpaper:") with replacing
		end repeat
	end try
end tell

what would be immensely helpful is if I could get the POSIX path without the “/” in front. Every time I try to open a filename using the quoted form of the POSIX path, I get an error message saying that “/Filename” does not exist. Which is true, because the file is not named “/Filename”, it’s named “Filename”
:confused:

Is it something like this you mean?


on open
	set olddelims to AppleScript's text item delimiters -- save their current state
	tell application "Finder" to set fileList to the selection
	set searchStrings to {"Aunt", "Uncle", "Grandmother"}
	set replaceStrings to {"Pam", "Bill", "Jeanette"}
	repeat with aFile in fileList
		-- EDIT
		tell application "Finder" to set fileContainer to (container of aFile) as alias
		-- 
		set fileName to name of aFile
		repeat with i from 1 to count of searchStrings
			set AppleScript's text item delimiters to item i of searchStrings
			set newName to every text item of fileName
			set AppleScript's text item delimiters to item i of replaceStrings
			set fileName to newName as string
			-- EDIT
			set newFilePath to (fileContainer as text) & "newNamem"
			set newFilePath to POSIX path of newFilePath
			do shell script "open " & quoted form of newFilePath
			--
		end repeat
		set name of aFile to fileName
	end repeat
	set AppleScript's text item delimiters to olddelims --return to old state
end open

yes, I think I can make that work. Excellent. Thanks :smiley: