applescript not detecting .dat extensions...

Curious.

I tried the original script on my 10.9.5 system, and it worked.

This works too (in both directions):


tell application "Finder"
	set targetFolder to insertion location as alias
	set name extension of files of targetFolder whose name extension is "txt" to "dat"
	set name extension of files of targetFolder whose name extension is "dat" to "txt"
end tell

And this works:


set thePath to path to desktop

set ext to "dat"
set extNew to "csv"

# set ext to "csv"
# set extNew to "dat"

tell application "Finder" to set name extension of (files of thePath whose name extension is ext) to extNew

This works:


set originalNameExtension to "dat"
set newNameExtension to "csv"

tell application "Finder"
	set targetFolder to insertion location as alias
	set fileList to (files of targetFolder whose name extension is originalNameExtension) as alias list
	repeat with i in fileList
		set name extension of i to newNameExtension
	end repeat
end tell

The method I’m most likely to employ personally uses the Satimage.osax for regular expression support and file globbing.

It’s very fast, and regular expressions make renaming very flexible.


set originalNameExtension to "csv"
set newNameExtension to "dat"

tell application "Finder" to set targetFolder to insertion location as alias

set fileList to glob ("*" & originalNameExtension) from targetFolder as alias

if fileList ≠ {} then
	set AppleScript's text item delimiters to return
	copy fileList as text to nameList
	set nameList to paragraphs of (change "^.+:(.+\\.)" & originalNameExtension into "\\1" & newNameExtension in nameList with regexp without case sensitive)
	set _cntr to 0
	
	tell application "Finder"
		repeat with i in fileList
			set _cntr to _cntr + 1
			set name of i to item _cntr of nameList
		end repeat
	end tell
	
end if

Then we have the old school method using Bash (shell script not AppleScript):

#! /usr/bin/env bash

cd ~/"test_directory/files-25-dat copy";

old=dat;
new=csv;

for file in *."$old"
	do
		mv "$file" "${file%.$old}.$new";
	done

Run directly from FastScripts it’s very quick.

  • Note the directory is my test directory and not the desktop.

Ordinarily I dislike hard-coding locations for scripts and prefer to work with the insertion location which will target the front window or the desktop if no windows are open.

#! /usr/bin/env bash

old=dat;
new=csv;

read -r -d '' aplScpt <<'EOF'
	try
		tell application "Finder" to set targetDir to insertion location as alias
		return POSIX path of targetDir
	on error
		return "false"
	end try
EOF

DIR=$(osascript -e "$aplScpt");

if [ ! "$DIR" = "false" ]; then
	cd "$DIR";
	for file in *."$old"
		do
			mv "$file" "${file%.$old}.$new";
		done
fi

This again is straight-up Bash not AppleScript, although it contains some AppleScript.

Once again I’m running directly from FastScripts.

We haven’t solved the reason why your original script stopped working. If you figure that out please let us know.

In any case you now have a solid range of alternatives.