Re: Copying the path of files selected in Finder
Re: Copying the path of files selected in Finder
- Subject: Re: Copying the path of files selected in Finder
- From: Yvan KOENIG <email@hidden>
- Date: Sat, 29 Apr 2017 15:05:48 +0200
Le 29 avr. 2017 à 13:24, Jean-Christophe Helary <email@hidden> a écrit :
>
> On Apr 29, 2017, at 19:18, Yvan KOENIG <email@hidden> wrote:
>
> 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
>
> I like that part :)
>
> Basically you set the TID to linefeed and by just coercing the list into text that adds linefeeds at the end of each list item? Is that how coercion works?
>
> Jean-Christophe
It's an old handler using the basic properties of text item delimiters.
text item delimiters
AppleScript provides the text item delimiters property for use in processing text. This property consists of a list of strings used as delimiters by AppleScript when it coerces a list to text or gets text items from text strings. When getting text items of text, all of the strings are used as separators. When coercing a list to text, the first item is used as a separator.
Note: PriortoOSXSnowLeopardv10.6,AppleScriptonlyusedthefirstdelimiterinthelistwhen getting text items.
Because text item delimiters respect considering and ignoring attributes in AppleScript 2.0, delimiters are case-insensitive by default. Formerly, they were always case-sensitive. To enforce the previous behavior, add an explicit considering case statement.
You can get and set the current value of the text item delimiters property. Normally, AppleScript doesn’t use any delimiters. For example, if the text delimiters have not been explicitly changed, the statement
returns the following:
For printing or display purposes, it is usually preferable to set text item delimiters to something that’s easier to read. For example, the script
{"bread", "milk", "butter", 10.45} as string
"breadmilkbutter10.45"
set AppleScript's text item delimiters to {", "}
{"bread", "milk", "butter", 10.45} as string
AppleScript Fundamentals
Global Constants in AppleScript
returns this result:
The text item delimiters property can be used to extract individual names from a pathname. For example, the script
returns the result "Release Notes".
If you change the text item delimiters property in Script Editor, it remains changed until you restore its previous value or until you quit Script Editor and launch it again. If you change text item delimiters in a script application, it remains changed in that application until you restore its previous value or until the script application quits; however, the delimiters are not changed in Script Editor or in other script applications you run.
My entire set is :
#=====
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
#=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
set l to text items of t
set AppleScript's text item delimiters to d2
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end remplace
#=====
(*
removes every occurences of d in text t
*)
on supprime(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 ""
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end supprime
#=====
When I'm sure that the system used is at least 10.10, I use modern versions using ASObjC.
use AppleScript version "2.4" # requires at least Yosemite
use scripting additions
use framework "Foundation"
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:
on replaceSeveralStringsByOne:sourceString existingStrings:listOfStrings newString:d1
set sourceString to current application's NSString's stringWithString:sourceString
repeat with d0 in listOfStrings
set sourceString to (sourceString's stringByReplacingOccurrencesOfString:d0 withString:d1)
end repeat
return sourceString as text
end replaceSeveralStringsByOne:existingStrings:newString:
on replaceSeveralStrings:sourceString existingStrings:listOfExistingStrings newStrings:listOfNewStrings
if (count listOfExistingStrings) > (count listOfNewStrings) then error "The first list of strings is longer than the second one !" number 1789
set sourceString to current application's NSString's stringWithString:sourceString
repeat with i from 1 to count listOfExistingStrings
set sourceString to (sourceString's stringByReplacingOccurrencesOfString:(listOfExistingStrings's item i) withString:(listOfNewStrings's item i))
end repeat
return sourceString as text
end replaceSeveralStrings:existingStrings:newStrings:
(*
# I prefer this one which doesn't need to compare the lengths of two lists.
set theString to "Pour qui sont ces serpents qui sifflent sur vos têtes"
set listOfCouples to {{"serpents", "vents"}, {"sifflent", "soufflent"}}
my replaceSeveralCouplesOfStrings:theString existingAndNewStrings:listOfCouples
*)
on replaceSeveralCouplesOfStrings:sourceString existingAndNewStrings:listOfCouples
set sourceString to current application's NSString's stringWithString:sourceString
repeat with i from 1 to count listOfCouples
set sourceString to (sourceString's stringByReplacingOccurrencesOfString:(item 1 of listOfCouples's item i) withString:(item 2 of listOfCouples's item i))
end repeat
return sourceString as text
end replaceSeveralCouplesOfStrings:existingAndNewStrings:
on replaceSeveralCharacters:sourceString existingChars:listOfChars newString:d1
set |⌘| to current application
set sourceString to |⌘|'s NSString's stringWithString:sourceString
set theDelims to (|⌘|'s NSArray's arrayWithArray:listOfChars)'s componentsJoinedByString:""
set setOfDelims to |⌘|'s NSCharacterSet's characterSetWithCharactersInString:theDelims
set anArray to (sourceString's componentsSeparatedByCharactersInSet:setOfDelims)
return (anArray's componentsJoinedByString:d1) as text
end replaceSeveralCharacters:existingChars:newString:
on split:sourceString usingString:d1
set sourceString to current application's NSString's stringWithString:sourceString
return (sourceString's componentsSeparatedByString:d1) as list
end split:usingString:
on splitOnAnySpace:someText -- free to a good home
set theString to current application's NSString's stringWithString:someText
set theList to theString's componentsSeparatedByCharactersInSet:(current application's NSCharacterSet's whitespaceAndNewlineCharacterSet())
return theList as list
end splitOnAnySpace:
on splitWithDelims:sourceString usingStrings:theDelims # theDelims is a list of characters
# makes an NSString from the string
set |⌘| to current application
set pathNSString to |⌘|'s NSString's stringWithString:sourceString
# makes an NSString from the list of delims and an NSCharacterSet from that
set theDelims to (|⌘|'s NSArray's arrayWithArray:theDelims)'s componentsJoinedByString:""
set setOfDelims to |⌘|'s NSCharacterSet's characterSetWithCharactersInString:theDelims
return (pathNSString's componentsSeparatedByCharactersInSet:setOfDelims) as list
--> {"Invoice", "invoicenumber", "clientid", "pdf"}
end splitWithDelims:usingStrings:
# set stringComponentList to my splitString:theString atCharactersInString:(linefeed & return & tab & space)
on splitString:someText atCharactersInString:stringOfChars -- free to a good home
set |⌘| to current application
set theString to |⌘|'s NSString's stringWithString:someText
set theList to theString's componentsSeparatedByCharactersInSet:(|⌘|'s NSCharacterSet's characterSetWithCharactersInString:stringOfChars)
return theList as list
end splitString:atCharactersInString:
on concatList:theList usingString:d1
set anArray to current application's NSArray's arrayWithArray:theList
return (anArray's componentsJoinedByString:d1) as text
end concatList:usingString:
Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) samedi 29 avril 2017 15:02:59
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden