AS Editor Replies show an error but the Result is correct. Why?

HI,
I have a third party app, which generates some folders, some empty and some with files. It also generates a text file listing the POSIX path of all those folders.
I’ve written an AS script to read the text file and then post-process the actual files from those folders. My script seems to work well doing what I intended it to do but, I just noticed a strange error while watching the script replies in the AS editor. Although the error

--> error number -1728 from POSIX file "/path/to/folder/"

is there, the actual result is correct. How strange !!! :expressionless:
This is a test script to show both the error in the replies and the correct result:


tell application "Finder"
	-- Make a test folder "myFolder" on your desktop and put any file in it.  I put "myFile.rtf" in it.
	-- The first line sets a list because I'm actually getting a list of 7 folders in POSIX format.
	set theFolders to {POSIX path of (path to desktop from user domain as string) & "myFolder/"}
	-- I convert the path to the Mac format because of the line after it.
	set mF to POSIX file (item 1 of theFolders) as string --> error number -1728 from POSIX file "/Users/username/Desktop/myFolder/"
	-- "every item of folder ." requires the folder in Mac format.
	set mF_items to every item of folder mF --> {document file "myFile.rtf" of folder "myFolder" of folder "Desktop" of folder "username" of folder "Users" of startup disk}
end tell

Please, can somebody explain to me:

  1. Why, notwithstanding the error in the script replies, the result of the script is correct?
  2. Is this statement in my script incorrect, and if yes, how should it be?
set mF to POSIX file (item i of theFolders) as string
  1. in the next script line, the statement
set mF_items to every item of folder mF

seems to work only with Mac path input. Is there a similar command accepting POSIX path input but, without getting any hidden files?
4. Since that error in the replies seems inconsequential, could I simply disregard it? :lol:

BTW, if the initial list would be in Mac path format,

set theFolders to {"Volume:Users:username:Desktop:myFolder:"

then the conversion from POSIX to Mac would not be required and thus there’d be no error in the script replies.
Unfortunately, that’s not how I get that list.

Any helpful comments and advice will be greatly appreciated.

Hello.

It is a little bit late for testing at the moment, but I do see a slash in the line below, which should have been a colon.

 set theFolders to {POSIX path of (path to desktop from user domain as string) & "myFolder/"}

I hope this at least partially explains the behaviour. I somehow seem to recollect that I have to coerce things to an alias before I try to get the posix path, but that may be wrong, though it usually works.

A quick glimpse into Finder’s dicitonary shows that the item, doesn’t have a Posix path property, so I’d coerce to an alias before getting the Posix path if I were you.

A bit of insomnia strike.

The returned error is a non fatal one issued because you trigger a Standard Addition (an Osax) feature in a tell application Finder block.

Two ways to get rid of it :

#1 :


tell application "Finder"
	-- Make a test folder "myFolder" on your desktop and put any file in it. I put "myFile.rtf" in it.
	-- The first line sets a list because I'm actually getting a list of 7 folders in POSIX format.
	set theFolders to {POSIX path of (path to desktop from user domain as string) & "myFolder/"}
	-- I convert the path to the Mac format because of the line after it.
	tell me to set mF to POSIX file (item 1 of theFolders) as string # EDITED
	-- "every item of folder ." requires the folder in Mac format.
	set mF_items to every item of folder mF --> {document file "myFile.rtf" of folder "myFolder" of folder "Desktop" of folder "username" of folder "Users" of startup disk}
end tell

In this one, thanks to the « tell me to » predicat, the offending instruction no longer issue the non fatal error.

#2 :


-- Make a test folder "myFolder" on your desktop and put any file in it. I put "myFile.rtf" in it.
-- The first line sets a list because I'm actually getting a list of 7 folders in POSIX format.
set theFolders to {POSIX path of (path to desktop from user domain as string) & "myFolder/"}
-- I convert the path to the Mac format because of the line after it.
set mF to POSIX file (item 1 of theFolders) as string # Here it's O
tell application "Finder"
	-- "every item of folder ." requires the folder in Mac format.
	set mF_items to every item of folder mF --> {document file "myFile.rtf" of folder "myFolder" of folder "Desktop" of folder "username" of folder "Users" of startup disk}
end tell

In the second one, the « offending » instruction is no longer in the tell application block so it no longer issue the non fatal error.

Now I will retry to sleep.

Yvan KOENIG (VALLAURIS, France) mercredi 30 janvier 2013 01:46:59

My source is a text file containing 7 folders with the POSIX path ending with a slash. I did actually try to remove the end slash but it didn’t make any difference.

I will try this to see how it works but, I’d probably prefer one of Yvan solution below.

I hope I didn’t trigger it :wink: although I’m known to suffer of similar strikes :confused:

I didn’t know about this behaviour. I learned something new today.

Many thank for the two solutions :slight_smile: I’ll go for the first one in the real script, because I have already too many tell blocks there. But I keep both solutions in my library of AS tricks.

Hello

You looked too fast.

 set theFolders to {POSIX path of (path to desktop from user domain as string) & "myFolder/"}

concatenate the string returned by :
POSIX path of (path to desktop from user domain as string)
which is the Unix path : “/Users/userAccount/Desktop/myFolder/”

and the string :
“myFolder/”

The result is the list theFolders containing an Unix path :
{“/Users/userAccount/Desktop/myFolder/myFolder/”}

The instruction :

tell me to set mF to POSIX file (item 1 of theFolders) as string

does it’s duty and returns :

“Macintosh HD:Users:userAccount:Desktop:myFolder:”

As I do my best to work without the Finder, I would code :


set theFolders to {POSIX path of (path to desktop from user domain as string) & "myFolder/"}

tell application "System Events"
item 1 of theFolders
	set mf_items to path of disk items of folder result whose visible is true
end tell

alternate


tell application "System Events"
	set theFolders to {(path to desktop from user domain as string) & "myFolder:"}
	item 1 of theFolders
	set mf_items to path of disk items of folder result whose visible is true
end tell

Yvan KOENIG (VALLAURIS, France) mercredi 30 janvier 2013 21:02:58

I’m puzzled by the behavior of POSIX path.


# part 1 behave flawlessly
set theFolders to {POSIX path of (path to desktop from user domain as string) & "myFolder/"}

tell application "System Events"
	-- some code
end tell

# part 2 behaves flawlessly
set pTod to path to desktop from user domain as string
tell me
	POSIX path of (pTod)
end tell
tell application "System Events"
	set theFolders to {result & "myFolder/"}
	-- some code
end tell

# part 3 fails
set pTod to get (path to desktop from user domain as string)
tell application "System Events"
	tell me
		POSIX path of pTod # FAILS HERE
		-->  "Il est impossible d'obtenir POSIX path of \"Macintosh HD:Users:yvankoenig:Desktop:\"." number -1728 from «class posx» of "Macintosh HD:Users:yvankoenig:Desktop:"
	end tell
	set theFolders to {result & "myFolder/"}
	-- some code
end tell

In the 3rd part, I carefully wrapped POSIX path of pTod in a tell me . end tell block
but I get the well known error -1728 which this time is a fatal one.

Am’I facing a normal behavior or a bug ?

Yvan KOENIG (VALLAURIS, France) mercredi 30 janvier 2013 21:35:02

Salu Yvan,

POSIX path is a (direct) property of class alias, so the tell me statement is useless (and causes the error).

As path to returns an alias specifier the as string parameter (it’s indeed a parameter not a coercion) is not needed too. This is sufficient

POSIX path of (path to desktop) -- user domain is the default domain 

Consider also that System Events has included most of the path to references

tell application "System Events" to get item 1 of desktop folder

Hello Stephan

I know that path to desktop is sufficient,
I just grabbed the instructions from the code posted by the OP.

My understanding is that this one was facing a problem in a large script.
He extracted some instructions showing what was failing.
I’m quite sure that the instruction (path to documents folder from user domain as string)
was written in a snap to build a pathname to a container hosting a folder to scan.
So I didn’t pay particular attention upon the detailed syntax which is not relevant in the true problem.

The OP’s one is now solved but

I’m now trying to understand why POSIX path fails when it is called in a tell application “System Events” block.

According to your message, I removed the tell me . end tell wrapper and got the same error message.


set pTod to get (path to desktop from user domain as string)
tell application "System Events"
	
	POSIX path of pTod # FAILS HERE
	--> "Il est impossible d'obtenir POSIX path of \"Macintosh HD:Users:yvankoenig:Desktop:\"." number -1728 from «class posx» of "Macintosh HD:Users:yvankoenig:Desktop:"
	-- some code
end tell

What is puzzling me is the fact that

POSIX path of (path to desktop as text)
behaves well outside a tell application “System Events” wraper
behaves well inside a tell application “Finder” wraper
but fails inside a tell application “System Events” wraper

In fact inside Tell application “System Events” . end tell
POSIX path requires a reference to an item


tell application "System Events"
	path of desktop folder # returns a string
	POSIX path of (disk item result)
end tell

is OK


tell application "System Events"
	path of desktop  # returns a folder ref
	POSIX path of result
end tell

is OK


path to desktop # returns an alias
tell application "System Events"
	POSIX path of result
end tell

is OK


path to desktop as text
tell application "System Events"
	POSIX path of result
end tell

fails with error -1728

Yvan KOENIG (VALLAURIS, France) mercredi 30 janvier 2013 23:03:37

the Finder is more tolerant

set pTod to get (path to desktop from user domain as string)
tell application "Finder"
	POSIX path of pTod # WORKS
end tell

Actually the troublemaker is System Events

all these forms work

set pTod to get (path to desktop from user domain as string)
tell me
	POSIX path of pTod # WORKS
end tell
set pTod to get (path to desktop from user domain as string)
tell application "Finder"
	tell me
		POSIX path of pTod # WORKS
	end tell
end tell

and of course the “designated” form

set pTod to get (path to desktop from user domain as string)
POSIX path of pTod # WORKS

This is one of the few things I hate about AppleScript. There is no consistency.

OK Stefan

As I am an old ape, I don’t reach the « hate it » level.
I just dislike such behavior because, as I’m always trying to understand, I waste time when I face one of them. :rolleyes:

Yvan KOENIG (VALLAURIS, France) jeudi 31 janvier 2013 11:55:37