applescript not detecting .dat extensions...

ok, did more testing with a different script just to see if it was recognizing it

set thisLocation to POSIX path of (path to desktop)

tell application "Finder"
	
	set myFileNames to name of files of folder (thisLocation as POSIX file) whose name extension is "dat"
	
end tell

and i keep getting:

but when i change the file on the desktop to any other extension (i’ve tried csv, xlsx, pdf, jpg, and many more) it pulls it with ZERO problems. so, my system suddenly stopped recognizing dat as a file extension???

Hi,

on my machine (Yosemite) the script works.

The path to the current desktop folder is the “root” folder of the Finder and there is a appropriate property.


tell application "Finder"
   set myFileNames to name of files of desktop whose name extension is "dat"
end tell

or even


tell application "Finder"
   set myFileNames to name of files whose name extension is "dat"
end tell

not for me. when i run your script i get:

only thing i can figure is that something on my system took control of .dat files and suddenly finder doesn’t even recognize them. this is some weird s@#$

in Finder select a .dat file and press ⌘I. Maybe there is a hidden extension

nope

Hello.

Have you tried to use diskutil to repair and check permissions?

I also wonder if you have you set any acl’s?

Having files open in other apps shouldn’t cause problems, but if any such app, isn’t a regular Cocoa app that uses NSDocument, I believe it might cause problems, at least if the file is open.

i have repaired disk permissions and reboot also. still the same problem. and no, no application is opening the file, in fact, there’s no application on my computer that is capable of opening it. that’s why i change the extension to csv. i also tried changing permissions on the file to full read and write for everyone in case that might have been an issue, but no go with that either.

how would i check on the ACL thing though?

nevermind, i found it. it’s full access

Hello.

You may want to try to “cat” the file in a terminal window, and see if it opens there.

You may also want to check any extended attributes (“ls -@”) when you are in the terminal window.

Hopefully, you have a backup on TimeMachine, if it turns out that the file is corrupted. :confused:

Edit

Are you a member of the group admin? -You check that too in the Terminal window by issuing: groups .
If not, then you should change the group to one you are a member of, if you aren’t an administrative user at all times.

cat reads it just fine in terminal. and when i run the ls, nothing unusual shows up. and yes, i’m an admin.

What’s the result of this, it displays the name extension of all files of desktop


tell application "Finder" to set myFileExtensions to name extension of files of desktop
set {TID, text item delimiters} to {text item delimiters, return}
set myFileExtensions to myFileExtensions as text
set text item delimiters to TID
display dialog myFileExtensions

i have 3 files on my desktop. one is the dat file, then two of the applescripts that i was working on. it catches the scripts, but not the dat. well, it catches a file there (notice blank line) but not the extension

Hello.

Maybe there is an invisible character along with the dot (.) please try to delete the fileextension, in the file info window of it in Finder. and then try to re-enter it.

I guess, the name extension is empty if no application is associated in launch services to open the file
Try this workaround:


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

tell application "Finder" to set name extension of (files of desktop whose name ends with ext) to extNew

hmmm…that kind of worked. it resulted with the file being named: ADET_AAC0001_0623.dat.csv

then it’s impossible with an one-liner


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

tell application "Finder"
	set filesToRename to files of desktop whose name ends with ext
	repeat with aFile in filesToRename
		set fileName to name of aFile
		set name of aFile to text 1 thru -4 of fileName & extNew
	end repeat
end tell

that worked. thank you!!

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.