Le 3 janv. 2017 à 16:14, Pat Stanford < email@hidden> a écrit :
I am just a lurker here, but trying to work around this is a mistake.
There are some characters that should just never be used in file names. This include both the ones you are having trouble with, forward slashes and colons. Both are used as path delimiters, so there use as part of the file name will often confuse either a program or the system to think that part of the name is a folder instead of part of the file name.
You can probably figure out a way to work around this, but it is almost guaranteed to come back and bite you at some point in the future.
Suck it up and just accept that you need to use another separator. A dash (-) or and underscore (_) are often the best delimiters to use in file names.
My 2 cents.
Start by explaining that to Apple. In the installed system there are several files whose name contain slashes.
Are you guessing that the system authors built the internal conversion code for the pleasure ?
Look at what return a script like:
set aFile to (path to desktop as text) & "file/name/with/four/slashes.txt" POSIX path of aFile
--> "/Users/myHome/Desktop/file:name:with:four:slashes.txt"
In fact, there is no problem with the use of slashes in Hfs filenames as well as there is no problem with the use of colon in POSIX names.
We just need to know what we are doing. During the last three months my practice thought me that the most efficient tool to deal with that is ASObjC.
On entry of every piece of code related to files, we may use POSIX paths or URLs. With both of them, when we ask for a filename we get them in the POSIX format (embedded slashes replaced by colons) I know that the preferred format is URL but if I remember well, their use was implemented in 10.11 and I'm often asked for code supposed to be used under 10.10 or even 10.9. So most of the time, I use POSIX paths.
use AppleScript version "2.4" use framework "Foundation" use scripting additions
set aFolder to (path to desktop as text) set aFolder to text 1 thru -2 of aFolder --> "SSD 500:Users:myHome:Desktop" # I deliberately dropped the final colon set HfsFileName to "file/name/with/four/slashes.txt" set POSIXfolder to POSIX path of aFolder --> "/Users/myHome/Desktop"
set POSIXfileName to my replace:HfsFileName existingstring:"/" newString:":" set pathString to current application's NSString's stringWithString:POSIXfolder set fullPath to (pathString's stringByAppendingString:POSIXfileName) fullPath as text --> "/Users/myHome/Desktop/file:name:with:four:slashes.txt"
set theURL to current application's class "NSURL"'s fileURLWithPath:POSIXfolder set theURL to theURL's URLByAppendingPathComponent:POSIXfileName --> "/Users/myHome/Desktop/file:name:with:four:slashes.txt" theURL's |path| as «class furl» --> file "SSD 500:Users:myHome:Desktop:file/name/with/four/slashes.txt"
on replace:sourceString existingstring:d1 newString:d2 set sourceString to current application's NSString's stringWithString:sourceString return (sourceString's stringByReplacingOccurrencesOfString:d1 withString:d2) as text end replace:existingstring:newString:
When we must build a pathname, no need to check if the first component ends with a separator, ASObjC does the job for me. You may check, even if you disable the instruction dropping the final colon from aFolder, that the pathname will be perfectly built.
Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) mardi 3 janvier 2017 21:22:47
|