Name of selected file without the name extension

I am writing a script for a service that will get metadata quickly. There must be a better way of getting the file name without the name extension. Any other ideas to clean this up would be appreciated as well.

ExifTool by Phil Harvey must be installed
http://www.sno.phy.queensu.ca/~phil/exiftool/

tell application "Finder"
	
	set thePath to (path to documents folder as text) & "exifdata"
	if not (exists thePath) then
		do shell script "mkdir -p " & POSIX path of thePath & ""
	end if
	
	repeat with i from 1 to count of (selection as list)
		set theLocation to POSIX path of (item i of (selection as list) as alias)
		set theItem to item i of (selection as list)
		set theName to (name of theItem as string)
		set theCount to (count of theName) - ((count of (name extension of theItem as string)) + 1)
		set theName to text 1 thru theCount in theName
		set destLocation to "'" & POSIX path of thePath & "/" & theName & ".txt'"
		do shell script "exiftool -a '" & theLocation & "' >" & destLocation
		do shell script "open " & destLocation & ""
	end repeat
end tell

try this


set thePath to POSIX path of (path to documents folder) & "exifdata"
do shell script "mkdir -p " & quoted form of POSIX path of thePath

tell application "Finder" to set theSelection to selection
repeat with anItem in theSelection
	set theLocation to POSIX path of (anItem as text)
	set {name:fileName, name extension:nameExtension} to anItem
	set baseName to text 1 thru ((get offset of "." & nameExtension in fileName) - 1) of fileName
	set destLocation to quoted form of (thePath & "/" & baseName & ".txt")
	do shell script "exiftool -a " & quoted form of theLocation & " >" & destLocation
	do shell script "open " & destLocation
end repeat

Thanks Stefan. That is much cleaner. Just out of curiosity, is there no other way besides using offsets to isolate the name without the extension?

Cocoa/ASOC has a stringByDeletingPathExtension method,
but in vanilla AppleScript there are only the offset or “count minus count” ways

A variation I like (but still using offset) is the one below which can’t be confused by intermediate periods within the name: myFile.new.txt, for example.

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

This is exactly the version I used above :slight_smile:

True, Stefan; I didn’t look carefully. :lol:

I have never set values from a file information record before, thanks for showing me that !

I’m missing the shell version:

every paragraph of (do shell script "ls " & (quoted form of POSIX path of (choose folder)) & " | sed -e 's/\\.[[:alnum:]]*$//'")

EDIT: updated the expression so com.apple.Finder.plist will not result in only ‘com’.