use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
#=====
on getItsNameAndContainer(hfsPath) # Appelé par quatre instructions. Même résultat qu'avec Finder. System Events traite mal !
local enListe, leNom, contenant
log linefeed & "----- entre dans getItsNameAndContainer : " & hfsPath
set enListe to my decoupe(hfsPath, ":")
if hfsPath ends with ":" then
set leNom to enListe's item -2 -- "à faire / vite", même résultat qu'avec Finder. System Events traite mal !
set contenant to my recolle(items 1 thru -3 of enListe, ":") & ":" -- "SSD 500:Users:yvankoenig:Desktop: des AWWP: encore des AW/WP:"
else
set leNom to enListe's item -1 -- Même résultat qu'avec Finder. System Events traite mal !
set contenant to my recolle(items 1 thru -2 of enListe, ":") & ":"
end if # cheminDocPages…
log "--> " & my recolle({leNom, contenant}, ", ") & linefeed & "sort de getItsNameAndContainer" & linefeed
return {leNom, contenant}
end getItsNameAndContainer
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
on recolle(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
#=====
on originalGetNameAndContainer:PosixPath
local theURL, PosixContainer, PosixName
--|-- log "Entre dans getNameAndContainer"
set theURL to current application's |NSURL|'s fileURLWithPath:PosixPath
set PosixContainer to theURL's URLByDeletingLastPathComponent()
set PosixName to theURL's lastPathComponent()
# set {theResult, theNameHfs} to (theURL's getResourceValue:(reference) forKey:(current application's NSURLLocalizedNameKey) |error|:(missing value)) # pour un nom Hfs
set PosixContainer to POSIX path of (PosixContainer as text) # this conversion failed under 10.10
return {PosixName as text, PosixContainer}
end originalGetNameAndContainer:
#=====
# Edited version
on getNameAndContainer:PosixPath
local theURL, PosixContainer, PosixName
--|-- log "Entre dans getNameAndContainer"
set theURL to current application's |NSURL|'s fileURLWithPath:PosixPath
set PosixContainer to theURL's URLByDeletingLastPathComponent()
set PosixName to theURL's lastPathComponent()
# set {theResult, theNameHfs} to (theURL's getResourceValue:(reference) forKey:(current application's NSURLLocalizedNameKey) |error|:(missing value)) # pour un nom Hfs
set PosixContainer to PosixContainer's |path|() as text # This conversion works with 10.10, 10.11 & 10.12
# but CAUTION, Sierra fails to add the ending slash
if PosixContainer does not end with "/" then set PosixContainer to PosixContainer & "/"
return {PosixName as text, PosixContainer}
end getNameAndContainer:
#=====
on buildNewPath:PosixPath addThis:aString
set theURL to current application's |NSURL|'s fileURLWithPath:PosixPath
set PosixContainer to theURL's URLByDeletingLastPathComponent()
set origName to (theURL's lastPathComponent()) as text
set newPath to theURL's URLByDeletingLastPathComponent()'s URLByAppendingPathComponent:(origName & aString)
set newPath to newPath's |path| as text
if newPath does not end with "/" then set newPath to newPath & "/"
return newPath
end buildNewPath:addThis:
#=====
on buildTwoPaths:PosixPath addThose:twoStrings
set theURL to current application's |NSURL|'s fileURLWithPath:PosixPath
set PosixContainer to theURL's URLByDeletingLastPathComponent()
set origName to (theURL's lastPathComponent()) as text
set newPath1 to theURL's URLByDeletingLastPathComponent()'s URLByAppendingPathComponent:(origName & twoStrings's item 1)
set newPath2 to theURL's URLByDeletingLastPathComponent()'s URLByAppendingPathComponent:(origName & twoStrings's item 2)
set newPath1 to newPath1's |path| as text
if newPath1 does not end with "/" then set newPath1 to newPath1 & "/"
set newPath2 to newPath2's |path| as text
if newPath2 does not end with "/" then set newPath2 to newPath2 & "/"
return {newPath1, newPath2}
end buildTwoPaths:addThose:
#=====
on getContainer:PosixPath
local theURL, PosixContainer
--|-- log "Entre dans getNameAndContainer"
set theURL to current application's |NSURL|'s fileURLWithPath:PosixPath
set PosixContainer to theURL's URLByDeletingLastPathComponent()
set PosixContainer to PosixContainer's |path|() as text # It's this one which fails to put the slash
# ATTENTION, Sierra ne met pas le / final
return PosixContainer
end getContainer:
#===============
# My old code
set hfsPath to (path to desktop as text) & "2002 après copie:A-B 2002:a flat 2.pages"
set {pagesName, hfsPagesContainer} to my getItsNameAndContainer(hfsPath)
--> {"a flat 2.pages", "SSD 500:Users:yvankoenig:Desktop:2002 après copie:A-B 2002:"}
set {folderName, hfsContainerOfPagesContainer} to my getItsNameAndContainer(hfsPagesContainer)
--> {"A-B 2002", "SSD 500:Users:yvankoenig:Desktop:2002 après copie:"}
set nameForPDFAs to folderName & " PDF-A"
--> "A-B 2002 PDF-A"
set hfsPathToPDFAs to hfsContainerOfPagesContainer & nameForPDFAs & ":"
--> "SSD 500:Users:yvankoenig:Desktop:2002 après copie:A-B 2002 PDF-A:"
set nameForOthers to folderName & " others"
--> "A-B 2002 others"
set hfsPathToOthers to hfsContainerOfPagesContainer & nameForOthers & ":"
--> "SSD 500:Users:yvankoenig:Desktop:2002 après copie:A-B 2002 others:"
# After introducing some BridgePlus and ASObjC instructions for other tasks, I changed my code which became :
set PosixPath to POSIX path of ((path to desktop as text) & "2002 après copie:A-B 2002:a flat 2.pages")
set {pagesName, pagesContainer} to my originalGetNameAndContainer:PosixPath
--> {"a flat 2.pages", "/Users/yvankoenig/Desktop/2002 après copie/A-B 2002/"}
# It works with 10.11 and 10.12 but the conversion to AppleScript format failed under 10.10
# So I switched to the edited one to which I had to add the extraneous instruction adding the ending slash under Sierra
set {pagesName, pagesContainer} to my getNameAndContainer:PosixPath
--> {"a flat 2.pages", "/Users/yvankoenig/Desktop/2002 après copie/A-B 2002/"}
set {folderName, containerOfPagesContainer} to my getNameAndContainer:pagesContainer
--> {"A-B 2002", "/Users/yvankoenig/Desktop/2002 après copie/"}
set nameForPDFAs to folderName & " PDF-A"
--> "A-B 2002 PDF-A"
set posixPathToPDFAs to containerOfPagesContainer & nameForPDFAs & "/"
--> "/Users/yvankoenig/Desktop/2002 après copie/A-B 2002 PDF-A/"
set nameForOthers to folderName & " others/"
--> "A-B 2002 others"
set posixPathToOthers to containerOfPagesContainer & nameForOthers & "/"
--> "/Users/yvankoenig/Desktop/2002 après copie/A-B 2002 others/"
# Now, I'm planning to use :
set posixPathToPDFAs to my buildNewPath:pagesContainer addThis:" PDF-A"
--> "/Users/yvankoenig/Desktop/2002 après copie/A-B 2002 PDF-A/"
set posixPathToOthers to my buildNewPath:pagesContainer addThis:" others"
--> "/Users/yvankoenig/Desktop/2002 après copie/A-B 2002 others/"
# or
set {posixPathToPDFAs, posixPathToOthers} to my buildTwoPaths:pagesContainer addThose:{" PDF-A", " others"}
--> {"/Users/yvankoenig/Desktop/2002 après copie/A-B 2002 PDF-A/", "/Users/yvankoenig/Desktop/2002 après copie/A-B 2002 others/"}
# Or
set posixPagesContainer to my getContainer:PosixPath
--> "/Users/yvankoenig/Desktop/2002 après copie/A-B 2002" # without the slash under Sierra
set posixPathToPDFAs to my buildNewPath:posixPagesContainer addThis:" PDF-A"
--> "/Users/yvankoenig/Desktop/2002 après copie/A-B 2002 PDF-A/"
set posixPathToOthers to my buildNewPath:posixPagesContainer addThis:" others"
--> "/Users/yvankoenig/Desktop/2002 après copie/A-B 2002 others/"
# Only posixPathToPDFAs and posixPathToOthers require the slash for later use
I apologize, I was forced to remove the formatting because the message was too long.
Yvan KOENIG running Sierra 10.12.0 in French (VALLAURIS, France) vendredi 23 septembre 2016 18:46:53