Sunday, April 21, 2019

#1 2009-10-29 04:01:30 pm

Schmye Bubbula
Member
Registered: 2003-11-23
Posts: 114

How to form a generalized pathname?

I have an operation I want to perform on different-named hard drives.
But file or alias HFS+ pathnames have to be explicit, right?
But POSIX paths are generic.
So searching around here I discovered that one can convert a POSIX path to HFS+ via:

POSIX file "[POSIX specification]" as Unicode text


So I made a test file on my Desktop and tried the following:

Applescript:

tell application "Finder" to move alias POSIX file "~/Desktop/test_file.txt" as Unicode text to the trash

But I got the error:

Can’t make alias (file \":~:Desktop:test_file.txt\") of application \"Finder\" into type Unicode text.


But seeing immediately above what AppleScript converted the POSIX path into, I said, "Aha! There *is* a generic form of HFS+ pathname, in this case, ':~:Desktop:test_file.txt' - in fact, it's just like POSIX except it uses colons instead of slashes."
But no, I can't even get this directly to work:

Applescript:

tell application "Finder" to move alias ":~:Desktop:test_file.txt" to the trash

How do I tell the Finder to, e.g., move an otherwise known filename to the trash if I need the AppleScript to work on an arbitrarily-named hard disk?


Filed under: Finder

Offline

 

#2 2009-10-29 05:38:27 pm

Schmye Bubbula
Member
Registered: 2003-11-23
Posts: 114

Re: How to form a generalized pathname?

Thanks and a tip o' the hat, Tom, that worked!

home as text


...is key. It covers items in my Home folder, but what variable more generally denotes the root level of my hard disk? So I could do something to a file in, for example:

/Library/Fonts/


Edit: Wait a minute, I think I know:

name of startup disk


...right?
Edit #2: Oops, I get an error, "Can't get name of startup disk."
"name of startup disk as text" errors as, "Can't make name of startup disk into type Unicode text." Rats!
Edit #3: I got it now: "path to startup disk"

Also, I'm academically interested in why the:

:~:Desktop:test_file.txt


...that I stumbled on didn't work as I attempted.

Last edited by Schmye Bubbula (2009-10-29 06:00:21 pm)

Offline

 

#3 2009-10-30 02:24:43 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11555
Website

Re: How to form a generalized pathname?

Hi,

first of all, AppleScript works with HFS paths (colon separated). Shortcuts like the tilde for the home folder don't work.

in Scripting Addtions there is the path to command, which provides relative references to the most important folders, for example path to desktop is the alias of the desktop of the current user

Applescript:


set fileToDelete to ((path to desktop as text) & "test_file.txt")
tell application "Finder" to delete file fileToDelete

Last edited by StefanK (2009-10-30 02:25:48 am)


regards

Stefan


Filed under: Finder

Offline

 

#4 2009-10-30 09:09:58 am

Schmye Bubbula
Member
Registered: 2003-11-23
Posts: 114

Re: How to form a generalized pathname?

StefanK wrote:

...first of all, AppleScript works with HFS+ paths (colon separated). Shortcuts like the tilde for the home folder don't work....


Thanks for the reply, Stefan!
I recognize that that AppleScript works directly only with HFS+ paths, which was why in my original post I expressed delight in discovering the POSIX-to-HFS+ conversion syntax:

POSIX file "[POSIX specification]" as Unicode text


The only reason I brought it up in the first place was because POSIX path specification doesn't depend on the name of the hard drive volume. As aforementioned that I discovered while looking around, and as you also pointed out to me, the "path to" syntax makes possible such generic HFS+ designations, too, which has been very helpful.
Now all that's left of my original query is the following:
While stumbling around trying things out, I saw that the result returned of, for example:

POSIX file "~/Desktop/test_file.txt" as Unicode text


...was:

:~:Desktop:test_file.txt


...which appeared to me to be an alternative designation syntax for HFS+ pathnames. So my remaining question is:

Applescript:

tell application "Finder" to move alias ":~:Desktop:test_file.txt" to the trash

I'm academically curious, why doesn't that work? And as a practical matter, knowing how correctly to write HFS+ pathnames in that form would permit operations using that generic syntax (i.e., not having to know the name of the hard drive) for pathnames that aren't covered by the limited list of common "path to" locations available.

Last edited by Schmye Bubbula (2009-10-30 09:11:59 am)


Filed under: Finder

Offline

 

#5 2009-10-30 09:19:33 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11555
Website

Re: How to form a generalized pathname?

as mentioned above, the tilde (~) doesn't work as a replacement for the home folder in HFS paths.
POSIX file is only needed if the source path is a POSIX path

Read the dictionary of Standard Scripting Additions to get the list which folders can be addressed relatively.
For example

path to desktop --> alias "[startup disk]:Users:[current user]:Desktop:"
path to home folder --> alias "[startup disk]:Users:[current user]:"
path to startup disk --> alias "[startup disk]:"
path to library folder --> alias "[startup disk]:Library:"
path to library folder from user domain --> alias "[startup disk]:Users:[current user]:Library:"

Last edited by StefanK (2009-10-30 09:21:53 am)


regards

Stefan

Offline

 

#6 2009-10-30 09:50:29 am

Schmye Bubbula
Member
Registered: 2003-11-23
Posts: 114

Re: How to form a generalized pathname?

StefanK wrote:

as mentioned above, the tilde (~) doesn't work as a replacement for the home folder in HFS paths.
POSIX file is only needed if the source path is a POSIX path...


Yes, I understand completely. I apologize for making you repeat yourself, evidently due to my utter failure to express myself clearly.
This all started because originally I didn't have in my bag of tricks:

path to startup disk


...which provides for abstraction from the particular name of a hard disk, allowing an AppleScript to work for anyone.
But during my haphazard search among these forums, I discovered the routine for *conversion* from POSIX to HFS+:

POSIX file "[POSIX specification]" as Unicode text


...the result of which for, e.g., the POSIX "~/Desktop/test_file.txt" is, in HFS+:

:~:Desktop:test_file.txt


...which surprised me with the apparent existence of yet another syntax for expressing HFS+ pathnames which I'd never seen before. Yes, "path to startup disk" does the job and covers all possibilities, and I'm ecstatic to have it under my belt, but now I'm just academically curious as how to incorporate the above colon-tilde form of HFS+ specification into an AppleScript. Anyone know more about it?

Offline

 

#7 2009-10-30 09:55:19 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11555
Website

Re: How to form a generalized pathname?

~ = path to home folder as text
~/Desktop/test_file.txt" = ((path to home folder as text) & "Desktop:test_file.txt")
or
~/Desktop/test_file.txt" = ((path to desktop folder as text) & "test_file.txt")

a tilde in conjunction with POSIX file does NOT work

Last edited by StefanK (2009-10-30 09:57:34 am)


regards

Stefan

Offline

 

#8 2009-10-30 10:24:46 am

Schmye Bubbula
Member
Registered: 2003-11-23
Posts: 114

Re: How to form a generalized pathname?

StefanK wrote:

~ = path to home folder as text
~/Desktop/test_file.txt" = ((path to home folder as text) & "Desktop:test_file.txt")
or
~/Desktop/test_file.txt" = ((path to desktop folder as text) & "test_file.txt")

a tilde in conjunction with POSIX file does NOT work


Yes, I have comprehended all along every last thing you so kindly have taken the time to spell-out for me. But I'm not talking about a tilde in conjunction with a POSIX filepath; rather, I'm inquiring about the returned tilde involved in the HFS+ filepath *converted* from a POSIX filepath by:

POSIX file "[POSIX specification]" as Unicode text


...which I learned about here ("POSIX path to AppleScript path"):
https://secure.macscripter.net/viewtopic.php?pid=22351
For example, the POSIX filepath "~/Desktop/test_file.txt" gets converted by it into an HFS+ filepath thusly:

:~:Desktop:test_file.txt


There's a tilde in there, and it's now an HFS+ filepath, no? Am I missing something?

Offline

 

#9 2009-10-30 10:35:11 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11555
Website

Re: How to form a generalized pathname?

Schmye Bubbula wrote:

thusly:

:~:Desktop:test_file.txt


There's a tilde in there, and it's now an HFS+ filepath, no? Am I missing something?


It's not really a HFS file path. HFS file paths start always with a disk name.
POSIX file tries to convert almost everything, but the scripter is responsible for a meaningful source wink
Either use full POSIX paths (without any abbreviations like a tilde) or one of the AppleScript path to shortcuts

Last edited by StefanK (2009-10-30 10:36:59 am)


regards

Stefan

Offline

 

#10 2009-10-30 11:01:34 am

Schmye Bubbula
Member
Registered: 2003-11-23
Posts: 114

Re: How to form a generalized pathname?

Ah, OK, I think I get it now. My "hand-holding" expectations were too high for the "POSIX file" function. It's stupid-in, stupid-out. I thought I was on to something, but you've got me straightened-out now. Thanks for *your* patient hand-holding, Stefan!

Offline

 

#11 2019-02-15 10:33:40 pm

Mr. Science
Member
From:: Satellite Beach, Florida
Registered: 2015-08-13
Posts: 43
Website

Re: How to form a generalized pathname?

Here is a one liner for fixing posix paths that begin with a tilde. The build steps are shown, but if you need a quick solution just copy the long line near the end. Conversions are included for posix, hfs, and verbose descriptive paths.

Applescript:


# EXAMPLE of resolving a TILDEd PATH...

#test using random versions of posix paths...
tell application "Finder" to set {strpdsk, usrnm} to {name of startup disk, name of home}
set psxPTH to some item of {"~/Library/Scripts/", "/Users/" & usrnm & "/Library/Scripts/", "/Volumes/" & strpdsk & "/Users/" & usrnm & "/Library/Scripts/"}

###EXAMPLE OF OCCASIONAL WRONG RESULT...
--tell application "Finder" to return psxPTH as POSIX file
---yay--> file "Macintosh_Harddrive:Users:Mr.Science:Library:Scripts:"
---grr---> file ":~:Library:Scripts:"

###EXAMPLE OF 4 STEPS TO A BETTER RESULT...
#1. flag the tilde!
-- if psxPTH contains "~" then ...[do something!]

#2. build a replacement for the tilde!...
--set psxPTH_part1 to POSIX path of (POSIX path of (path to home folder))
--return psxPTH_part1 ---> "/Users/Mr.Science/"

#3. snip off the tilde!...
--set psxPTH_part2 to (characters (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH) as string
--return psxPTH_part2 ------>"Library/Scripts/"

#4. put methods 1+2+3 together into a one-liner...
if psxPTH contains "~" then set psxPTH to ((POSIX path of (POSIX path of (path to home folder))) & (characters (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH) as string)
------>"/Users/Mr.Science/Library/Scripts/"

--other steps after the posix is resolved...

set hfsPTH to (POSIX file psxPTH) as text
------>"Macintosh_Harddrive:Users:Mr.Science:Library:Scripts:"

set alsPTH to alias hfsPTH
------> alias "Macintosh_Harddrive:Users:Mr.Science:Library:Scripts:"

tell application "Finder" to set vrbspth to item alsPTH
------> folder "Scripts" of folder "Library" of folder "Mr.Science" of folder "Users" of startup disk of application "Finder"

Browser: Safari 605.1.15
Operating System: macOS 10.14


"Fail and fail until you fail to fail!"   ~   http://www.theMrScienceShow.com


Filed under: PATH, POSIX, Hfs, tilde

Offline

 

#12 2019-02-16 05:49:53 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5626

Re: How to form a generalized pathname?

Mr. Science wrote:

Applescript:


#3. snip off the tilde!...
--set psxPTH_part2 to (characters (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH) as string
--return psxPTH_part2 ------>"Library/Scripts/"



This is not a good way to extract a substring. Apart from being inefficient -- you are creating a list, and then coercing it to a string -- the result of the coercion depends on the value of AppleScript's text item delimiters at the time. Depending on the context, they could be anything. Better to do:

Applescript:

set psxPTH_part2 to text (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#13 2019-02-16 06:28:10 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11555
Website

Re: How to form a generalized pathname?

This is another one-liner wink

Applescript:

use framework "Foundation"

set aPath to "~/Library/Scripts/"
set expandedPath to ((current application's NSString's stringWithString:aPath)'s stringByExpandingTildeInPath()) as text

Last edited by StefanK (2019-02-16 10:10:26 am)


regards

Stefan

Offline

 

#14 2019-02-17 06:37:37 am

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 55

Re: How to form a generalized pathname?

There are a number of reasons to use System Events rather than Finder to handle file system operations, one of them being that System Events can handle posix paths, including usage of the tilde:

Applescript:

set filepaths to {"~/Desktop/sample.txt", ¬
       "Macintosh HD:Users:CK:Desktop:sample.txt", ¬
       alias "Macintosh HD:Users:CK:Desktop:sample.txt", ¬
       POSIX file "/Users/CK/Desktop/sample.txt"}

set fileobjects to {}

tell application "System Events" to repeat with filepath in filepaths
       set end of fileobjects to the item named filepath as alias
end repeat

return fileobjects

Output:

Applescript:

{alias "Macintosh HD:Users:CK:Desktop:sample.txt", ¬
       alias "Macintosh HD:Users:CK:Desktop:sample.txt", ¬
       alias "Macintosh HD:Users:CK:Desktop:sample.txt", ¬
       alias "Macintosh HD:Users:CK:Desktop:sample.txt"}

Offline

 

#15 2019-02-17 07:16:08 am

Mr. Science
Member
From:: Satellite Beach, Florida
Registered: 2015-08-13
Posts: 43
Website

Re: How to form a generalized pathname?

Shane Stanley wrote:
Mr. Science wrote:

Applescript:


#3. snip off the tilde!...
--set psxPTH_part2 to (characters (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH) as string
--return psxPTH_part2 ------>"Library/Scripts/"



This is not a good way to extract a substring. Apart from being inefficient -- you are creating a list, and then coercing it to a string -- the result of the coercion depends on the value of AppleScript's text item delimiters at the time. Depending on the context, they could be anything. Better to do:

Applescript:

set psxPTH_part2 to text (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH



Thanks Shane, good catch!


"Fail and fail until you fail to fail!"   ~   http://www.theMrScienceShow.com

Offline

 

#16 2019-02-17 09:47:00 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3397

Re: How to form a generalized pathname?

In step 4, I don't understand the need for the double POSIX path component.
It seems that it may be cleaned as :

Applescript:

if psxPTH contains "~" then set psxPTH to POSIX path of (path to home folder) & text (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 17 février 2019 16:46:35

Last edited by Yvan Koenig (2019-02-17 09:49:23 am)

Offline

 

#17 2019-02-17 02:37:48 pm

Mr. Science
Member
From:: Satellite Beach, Florida
Registered: 2015-08-13
Posts: 43
Website

Re: How to form a generalized pathname?

Yvan Koenig wrote:

In step 4, I don't understand the need for the double POSIX path component.
It seems that it may be cleaned as :

Applescript:

if psxPTH contains "~" then set psxPTH to POSIX path of (path to home folder) & text (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 17 février 2019 16:46:35



You are correct. I guess I left that in after the mashup of the parts. Works fine without!

Applescript:


set psxPTH to "~/Library/Scripts/"

if psxPTH contains "~" then set psxPTH to POSIX path of (path to home folder) & text (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH

return psxPTH ---> "/Users/Mr.Science/Library/Scripts/"

Addendum: Upon further testing I realized it could be confused by inadvertent placement of a tilde elsewhere in the path. So this was a way to restrict the tilde flag to the beginning of the path...

Applescript:

--------- while this is ok -------------
set psxPTH to "~/Library/Scripts/"
if psxPTH contains "~" then set psxPTH to POSIX path of (path to home folder) & text (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH
return psxPTH ---> "/Users/Mr.Science/Library/Scripts/"

--------- this fails -------------
set psxPTH to "/Users/Mr.Science/Desktop/this~name~has~a~tilde/"
if psxPTH contains "~" then set psxPTH to POSIX path of (path to home folder) & text (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH
return psxPTH ---> "/Users/Mr.Science/ame~has~a~tilde/"
# NOTE that the above line got chopped up!

--------- so this is better -------------
set psxPTH to "/Users/Mr.Science/Desktop/this~name~has~a~tilde/"
if character 1 of psxPTH contains "~" then set psxPTH to POSIX path of (path to home folder) & text (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH
return psxPTH ---> "/Users/Mr.Science/Desktop/this~name~has~a~tilde/"

Basically, it can now ignore paths that don't require the coercion.

Last edited by Mr. Science (2019-02-17 03:13:39 pm)


"Fail and fail until you fail to fail!"   ~   http://www.theMrScienceShow.com

Offline

 

#18 2019-04-18 04:20:52 pm

Mr. Science
Member
From:: Satellite Beach, Florida
Registered: 2015-08-13
Posts: 43
Website

Re: How to form a generalized pathname?

Following up; I don't know how effective this is across other releases of OS X but here are four simple one-liners that convert a tilded path to four other path types. Random samples are used for each run. 

Applescript:


set pth to some item of {".", "..", "~", "~/", "~//", "~/Desktop", "~/Desktop/", "~/Desktop//"}

tell application "System Events" to set psxPTH to (POSIX path of (alias pth))
tell application "System Events" to set hfsPTH to path of (alias (POSIX path of (alias pth)))
tell application "System Events" to set alsPTH to (path of (alias (POSIX path of (alias pth)))) as alias
tell application "System Events" to set vrbPTH to alias (path of (alias (POSIX path of (alias pth)))) of application "Finder"

return {"Original path was: ''" & pth & "'' ", return, psxPTH, return, hfsPTH, return, alsPTH, return, vrbPTH, return}

#Sample result:

Applescript:


{"Original path was: ''~/Desktop'' ", "
"
, "/Users/Mr_Science/Desktop", "
"
, "MacPro_HD:Users:Mr_Science:Desktop:", "
"
, alias "MacPro_HD:Users:Mr_Science:Desktop:", "
"
, folder "Desktop" of folder "Mr_Science" of folder "Users" of startup disk of application "Finder", "
"
}


"Fail and fail until you fail to fail!"   ~   http://www.theMrScienceShow.com


Filed under: PATH, alias, POSIX, tilde

Offline

 

#19 2019-04-19 03:10:37 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3397

Re: How to form a generalized pathname?

Mr. Science wrote:


Applescript:


--------- so this is better -------------
set psxPTH to "/Users/Mr.Science/Desktop/this~name~has~a~tilde/"
if character 1 of psxPTH contains "~" then set psxPTH to POSIX path of (path to home folder) & text (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH
return psxPTH ---> "/Users/Mr.Science/Desktop/this~name~has~a~tilde/"

Basically, it can now ignore paths that don't require the coercion.



It would be cleaner with :

Applescript:

set psxPTH to "/Users/Mr.Science/Desktop/this~name~has~a~tilde/"
if psxPTH starts with "~" then set psxPTH to POSIX path of (path to home folder) & text (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH
return psxPTH ---> "/Users/Mr.Science/Desktop/this~name~has~a~tilde/"

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 19 avril 2019  11:10:21

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)