Testing for File Type

Hi All:

I’m trying to build a script that executes a set of commands on a file supplied by the user (via choose file). In order to do so properly, I need to test for the kind of file chosen. There are three options:

a) An Alias File
b) A .tex file
c) Anything else

How do I test for these three options? Suppose I’ve said
set chosenFile to (choose file)

I assume I’ll do something like:

If chosenFile [is an alias file] then
do something
If chosenFile [is a tex file] then
do something else
else cough up a hairball
end if

I’d appreciate any suggestions on what fills in the square brackets.

For what it’s worth, here’s the script that this is supposed to be a part of. I’ve indicated where the test for file type is supposed to come in.


set g to (choose file)
tell application "Finder"   -- here comes the file test...
       if g is an alias file then
	set a to original item of g
       if g is a .tex file then
        set a to g
       else
        [exit the script with an error message]
        set b to a's container as Unicode text --This gets the folder of the chosen file
	set c to a's name --The name of the file
	set d to a's name extension --The file extension of the file
end tell

set e to quoted form of POSIX path of b --This should deal with any spaces in the path, and give you a usable path for the terminal.

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ("." & d)
set f to c's text item 1 --This will effectively remove the dot and the file extension from the file name
set AppleScript's text item delimiters to astid
{b, c, d, e, f}

tell application "Terminal"
	do script "cd " & e & ¬
		"; pdflatex -interaction=nonstopmode " & c & ¬
		"; pdflatex -interaction=nonstopmode " & c & ¬
		"; pdflatex -interaction=nonstopmode " & c & ¬
		"; pdflatex -interaction=nonstopmode " & c & ¬
		"; bibtex " & f & ¬
		"; pdflatex -interaction=nonstopmode " & c & ¬
		"; pdflatex -interaction=nonstopmode " & c & ¬
		"; pdflatex -interaction=nonstopmode " & c
end tell

Thanks a lot,
Bernhard.

Bernhard:

I think this will take care of most of your if/then situation:

set g to (choose file)
tell application "Finder"
	set {file_Kind, file_Ext} to {g's kind, g's name extension}
	if file_Kind is "Alias" then
		set a to original item of g
	else
		if file_Ext is ".tex" then
			set a to g
		else
			display dialog "Quitting script" giving up after 1
			error number -128 --[exit the script with an error message]	
		end if
	end if
	set b to a's container as Unicode text --This gets the folder of the chosen file
	set c to a's name --The name of the file
	set d to a's name extension --The file extension of the file
	
end tell

Hope this helps.

Craig:

thanks a lot. That almost worked. The only problem was trying to test for the file extension “.tex” Somehow, the script couldn’t recognize it properly. When I chose a file with that extension, the script would exit. So I tested the file for being of kind “LaTeX Source File”, and now it works like a charm.

Cheers,
Bernhard.

Beautiful. Thanks for the feedback.

That’s probably because name extensions don’t include the ‘dot’ prefix, Bernhard. Testing for a “tex” extension should work for you. :slight_smile:

Arrgghh! I can’t believe I missed that. Thanks for the input, kai.

Easily done, Craig. It’s always the little fish that slip through the net. :slight_smile: