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:

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

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

But I got the error:

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:

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?

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

…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:

Edit: Wait a minute, I think I know:

…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:

…that I stumbled on didn’t work as I attempted.

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


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

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:

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:

…was:

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

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.

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:”

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:

…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+:

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

…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?

~ = 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:

…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:

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

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!

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.


# 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

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:

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

This is another one-liner :wink:

use framework "Foundation"

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

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:

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:

{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"}

Thanks Shane, good catch!

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

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!


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…

--------- 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.

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.


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:


{"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", "
"}

It would be cleaner with :

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

I find Shane’s filemanager lib very handy.

https://www.macosxautomation.com/applescript/apps/Script_Libs.html#FileManagerLib

Also NSURL and NSString have some great functions for building and disassembling
Paths.