full path problems

Hello this my first time posting and I want to thank everyone ahead of time for the great resource.
Now my issue
I am trying to get a full path to a folder that is read from a text file

I have a text file the has a folder location in the form of /Users/name/document/work
and I’m trying to get applescript to read the full path as it stands I can’t get it to return
the file path":

if its a mounted volume it workes fine

Thansk John

Hi,

AppleScript paths (or references) are different from unix type references. AppleScript uses “:” (colons) as object delimiters while unix uses “/” (forward slash). You can coerce unix type references to AppleScript references with ‘posix path’. Here’s an example:

set unix_path to "/Users/name/document/work "
set as_file_specification to unix_path as POSIX file
try
set as_alias_ref to as_file_specification as alias
on error – the file does not exist
beep 2
return
end try

You can see the difference between the two paths by just running the first two statements of the above script. The error handler is just used to see if as_file_specification is an existing file or folder. When trying to coerce the file specification to ‘alias’, if the file or folder does not exixt it will error and you will hear two beeps.

BTW: at the end of this statement:

set unix_path to "/Users/name/document/work "

notice that there is some character (looks like a space) after “work”. These things can cause problems in debugging. Is there suppose to be a space?

gl,

Hi,

My fault. I copied and pasted the space :-). I’ll die of congac soon if not later.

gl,

I also have full path problems, and more, as shown in the comments below:

	set cdScript to "cd " & itsFolder
	display dialog cdScript
	-- "cd /Volumes/iMac\ Internal\ HD/Users/johnlove/Library/
	--                Application\ Support/SuperDuper!/Saved\ Settings"
	do shell script (cdScript) -- works!
	
	-- not even sure if chmod +x is required ???
	
	-- even so, we've changed directories, so why is the full path required ???
	-- set execScript to "chmod +x " & theScript
	set execScript to "chmod +x " & itsFolder & "/" & theScript
	display dialog execScript
	-- "chmod +x /Volumes/iMac\ Internal\ HD/Users/johnlove/Library/
	--                Application\ Support/SuperDuper!/Saved\ Settings/
	--                set_icon_applescript_from_shell.scpt"
	do shell script (execScript) -- works only with full path
	
	-- same full path question ???
	-- set runScript to theScript & " \"" & theSrcDisk & "\" \"" & theDestDisk & "\""
	set runScript to itsFolder & "/" & theScript & ¬
		" \"" & theSrcDisk & "\" \"" & theDestDisk & "\""
	display dialog runScript
	-- /Volumes/iMac\ Internal\ HD/Users/johnlove/Library/
	--                 Application\ Support/SuperDuper!/Saved\ Settings/
	--                 set_icon_applescript_from_shell.scpt "iMac Internal HD" "LaCie_External_HD"
	do shell script (runScript) -- does not work = "cannot execute binary file"  ???

John Love

John Love,

Okay lets see if I can help out here… chmod +x well do you need to add execute privs ? If not, then you don’t need it.

As for why you need the full path because as soon as your first script finishes you have left the shell session. When your second script runs it is running from your default shell location, not where you cd’d to in the first script.

As for the last set of questions I’m not familiar with SuperDuper, but it looks like your trying to run an app incorrectly from the shell.

Is that an AppleScript you’re trying to run John?

Are you aware that you can get/set owner, owner privileges, group, group privileges, and everyones privileges in the Finder’s properties of a file faster than you can run a shell script to chmod or chown?

Actually, Adam, chmod is faster for me.

~400 milliseconds

set t1 to GetMilliSec

set theFile to quoted form of POSIX path of ((path to desktop as Unicode text) & "Permission Test.txt")

repeat 10 times
	do shell script "/bin/chmod 777 " & theFile
end repeat

return (GetMilliSec) - t1

~1100 milliseconds

set t1 to GetMilliSec

set theFile to alias ((path to desktop as Unicode text) & "Permission Test.txt")

repeat 10 times
	tell application "Finder"
		set owner privileges of theFile to read write
		set group privileges of theFile to read write
		set everyones privileges of theFile to read write
	end tell
end repeat

return (GetMilliSec) - t1

That said, chown requires superuser rights, so it may not be well suited to a distributed AppleScript.

Model: Mac mini
AppleScript: 1.10.7
Browser: Safari 2.0.4 (419.3)
Operating System: Mac OS X (10.4)

Is GetMilliSec a personal function of yours or a scripting addition?

Eiter way though what about placing the repeat within in Finder tell block, would that spead it up?

tell application "Finder"
	repeat 10 times
		set owner privileges of theFile to read write
		set group privileges of theFile to read write
		set everyones privileges of theFile to read write
	end repeat
end tell

I can’t figure out how to time in milliseconds so all 3 ways are going to fast for me to calculate.

While I wait for an answer to post #6.

Scripting Addition: http://osaxen.com/files/getmillisec1.0.1.html

No change for me. (An uneducated guess: the actual execution is probably the same or very similar.)

The point is to get a larger sampling of one set of operations. If you want to remove the AppleScript repeat loop (and the starting of all those shells), then you could do the loop in the shell script:

set t1 to GetMilliSec

set theFile to quoted form of POSIX path of ((path to desktop as Unicode text) & "Permission Test.txt")

-- Runs `chmod` 10 times
do shell script "for i in `/usr/bin/jot 10 1 10`;
do
	/bin/chmod 777 " & theFile & "
done"

return (GetMilliSec) - t1

That takes about ~115 milliseconds here.

Note that a minor change to the above script would make it change the permissions for a whole folder; Compare that to getting a list of files and looping over them in AppleScript.

To: James Nierodzik

Thanks for the guidance about:

"Note with execScript above, you need the full path again because as soon as your
"first script finishes, you have left the shell session. When your second script runs it
“is running from your default shell location, not where you cd’d to in the first script.”

So, learning from this, I simply:

	set comboScript to "cd " & itsFolder & "; " & "chmod +x " & theScript
	do shell script comboScript

Now, that you solved that problem, let’s go to the running of a AppleScript within a do shell script – and without calling

osascript << EOSA
 -- whatever
EOSA

What am I doing wrong in the following such that an error returns = “cannot execute binary file”:

	set runScript to itsFolder & "/" & theScript & ¬
		" \"" & theSrcDisk & "\" \"" & theDestDisk & "\""
	do shell script (runScript) -- doesn't work = "cannot execute binary file"  ???

The strings theScript, theSrcDisk and theDestDisk are defined earlier in the AppleScript. theScript has a

on run parms
	set theSrcDisk to item 1 of parms
	set theDestDisk to item 2 of parms
	copyAllFiles(theSrcDisk, theDestDisk)
end run

on copyAllFiles(src, dest)
-- whatever
end copyAllFiles

In finishing, you are absolutely correct if you are thinking that the following in AppleScript does the job:

	set runScript to load script file (chosenScript)
	
	tell runScript to copyAllFiles(theSrcDisk, theDestDisk)

However, my only excuse is to learn more.

John Love

You can’t execute an AppleScript directly in that fashion, John. (*nix shells don’t know anything about AppleScript. Thus the execute bit [chmod] is irrelevant.) Maybe you can try something like this:

get quoted form of POSIX path of ((path to application support folder as Unicode text) & "SuperDuper!:Saved Settings:")
do shell script "/usr/bin/osascript " & result -- arguments/parameters if needed

Also, why not use the run script command? (Oh, nevermind. :))

run script (alias ((path to application support folder as Unicode text) & "SuperDuper!:Saved Settings:"))

Thank you so much,

It looks like I’m back to what I began with about a week ago,

   set runScript to load script file (colonDelimitedScript)
   
   tell runScript to copyAllFiles(theSrcDisk, theDestDisk)

I don’t know about you, but I wouldn’t have traded this last week for anything simply because thanks to all of you I have learned a bunch and a half (about osascript, about combining calls in one do shell script, and even some stuff from “Unix for Dummies”).

A hundred rounds of applause to all of you.

John Love

That’s interesting, Bruce. On mine, the Finder version is very slightly faster, with both around 100 ms (using your scripts)

dual-core G5/2.3
AppleScript 1.10.7
OS X 10.4.7