POSIX file opening.

Hello,

I am trying to open a specific file based on a product code so I am using a lot of if statements and folders to open a file.

The file is only located 3 folders further down than the script its self.

I have noticed that if i use


tell application "Finder" to open POSIX file pathvariable & "/file.extention" 

it works fine but I still have to define the file as well in one variable along with the file path like

tell application "Finder" to open POSIX file pathandfilevariable

I get the error
Result:
error “Can’t get POSIX file of "path/file.extention”." number -1728 from POSIX file of “path/file.extention”

Any idea why this could be?

Thanks,
Bruce

It should be something like:

tell application "Finder"
	set var to POSIX file "/Users/admin/Desktop"
	open var
end tell

You’re missing parentheses:

tell application "Finder" to open POSIX file (pathvariable & "/file.extension")

Sorry for the late reply, I was typing one up yesterday but I had to leave and I lost it.

sorry for the confusion but I can get Finder to open the file if I set the path as a variable i.e.


tell application "System Events" to set parentFolder to POSIX path of container of (path to me)
set folder1 to "/folder1"
set myPath to parentFolder & folder1

tell application "Finder" to open POSIX file myPath & "/file.extention"

but what I am trying to do is.


tell application "System Events" to set parentFolder to POSIX path of container of (path to me)
set folder1 to "/folder1"
set fileDocument to "/file.extention"
set pathtoFile to parentFolder & folder1 & fileDocument

tell application "Finder" to open POSIX file pathtoFile

I get the error

“Can’t get POSIX file of “/Users/server/Desktop/folder1/file.extention”.”

I am trying to do this because something that the user selects from excel contains the file path and the file path exists because if I copy and past the file path from the error into tell application “Finder” to open POSIX file “path/from/error.extention” it works fine

EDIT:
There are 2 files to choose from at the end of every path possible.
here is the actual script I have been using and I am not sure what is wrong with it.


tell application "System Events" to set parentFolder to POSIX path of container of (path to me)
set ddResult to button returned of (display dialog "Select Product Code" buttons {"Cancel", "Next"} default button "Next" cancel button "Cancel")

tell application "Microsoft Excel"
	if ddResult = "Next" then
		set pcode to value of selection as text
	end if
end tell
display dialog "Mesh?" buttons {"No", "Yes"} default button "No"
if result = {button returned:"Yes"} then
	set labelFile to "mesh.laba"
else if result = {button returned:"No"} then
	set labelFile to "nomesh.laba"
end if
set openLabel to getLabel(pcode, parentFolder, labelFile)

--------------------
-->> LABEL HANDLER
--------------------

on getLabel(pcode, parentFolder, labelFile)
	if pcode contains "EF" then
		set path1 to "/EF"
	else if pcode contains "JF" then
		set path1 to "/JF"
	else if pcode contains "CF" then
		set path1 to "/CF"
	else if pcode contains "UF" then
		set path1 to "/UF"
	end if
	if pcode contains "LG" then
		set path2 to "/LG"
	else if pcode contains "MD" then
		set path2 to "/MD"
	else if pcode contains "ST" then
		set path2 to "/ST"
	else if pcode contains "SM" then
		set path2 to "/SM"
	end if
	if pcode contains "P" then
		set path3 to "/P/"
	else if pcode contains "E" then
		set path3 to "/E/"
	end if
	
	set labelPath to parentFolder & "/labels" & path2 & path1 & path3 & labelFile
	tell application "Finder" to open POSIX file of labelPath
end getLabel

EDIT 2: the path2 & path1 are the correct way round I found I made the folders the wrong way so for testing I just swapped them around from path1 & path2 to path2 & path1

Just found the solution (bit of a weird one)

If do


set foldervariable to /Users/user/desktop/folder1/folder2
set filevariable to "/file.extention"
set folderfilevariable to foldervariable & filevariable
tell application "Finder" open POSIX file folderfilevariable as string

It works although I still get “–> error number -1728 from POSIX file “/Users/user/desktop/folder1/folder2/file.extention”” in the log but the file opens.

Any idea why this could be?

Thanks,
Bruce

This is a version which works with HFS paths (colon separated) instead of POSIX paths.
If the file at the composed file path does not exist, a message will be displayed


tell application "System Events" to set parentFolder to path of container of (path to me)
set ddResult to button returned of (display dialog "Select Product Code" buttons {"Cancel", "Next"} default button "Next" cancel button "Cancel")

tell application "Microsoft Excel"
	if ddResult = "Next" then
		set pcode to value of selection as text
	end if
end tell
display dialog "Mesh?" buttons {"No", "Yes"} default button "No"
if result = {button returned:"Yes"} then
	set labelFile to "mesh.laba"
else if result = {button returned:"No"} then
	set labelFile to "nomesh.laba"
end if
set openLabel to getLabel(pcode)
set filePath to parentFolder & "labels" & ":" & openLabel & ":" & labelFile
tell application "Finder"
	if exists file filePath then
		open file filePath
	else
		display dialog "The file '" & filePath & "' does not exist"
	end if
end tell
--------------------
-->> LABEL HANDLER
--------------------

on getLabel(pcode)
	if pcode contains "EF" then
		set path1 to "EF"
	else if pcode contains "JF" then
		set path1 to "JF"
	else if pcode contains "CF" then
		set path1 to "CF"
	else if pcode contains "UF" then
		set path1 to "UF"
	end if
	if pcode contains "LG" then
		set path2 to "LG"
	else if pcode contains "MD" then
		set path2 to "MD"
	else if pcode contains "ST" then
		set path2 to "ST"
	else if pcode contains "SM" then
		set path2 to "SM"
	end if
	if pcode contains "P" then
		set path3 to "P"
	else if pcode contains "E" then
		set path3 to "E"
	end if
	
	return path2 & ":" & path1 & ":" & path3
	
end getLabel


Thanks for that.

That actually sorted out another problem that occurred.

I tried putting it into my main script and it just stopped work but now it does thanks for that!

Thanks,
Bruce

As far as I can tell, the Finder won’t recognize any POSIX commands. The above statement fails consistently for me.

I have tested, and found that if you have a POSIX path you want opened, then this will work:


tell application "Finder" to open posixPath as «class furl»

Credit all due to a good friend who is a true AppleScript guru.

I found solving this simple issue very frustrating :frowning: – not due to Shane, but due to Apple, and how they designed AppleScript. But it is what it is.

Hope this will help others avoid my frustration. :slight_smile:

I’m pretty sure that snippet worked when it was written (a couple of years ago), but the problem doesn’t relate to the Finder recognizing a POSIX path (which it doesn’t). POSIX file produces a «class furl», but it looks like you now need to use it as a coercion in this case, rather than as a specifier.

tell application "Finder" to open (pathvariable & "/Lorikeet.jpg") as POSIX file

POSIX file still seems to work outside a Finder tell block OK:

set theFile to POSIX file (pathvariable & "/Lorikeet.jpg")
tell application "Finder" to open theFile