text
stringlengths
0
15.7k
source
stringlengths
6
112
if not incInvis then set theOptions to 4 -- NSDirectoryEnumerationSkipsHiddenFiles
lns3.scpt
set {theURLs, theError} to NSFileManager's |defaultManager|()'s contentsOfDirectoryAtURL:theURL includingPropertiesForKeys:{} options:theOptions |error|:(reference)
lns3.scpt
if theURLs is missing value then error (theError's |localizedDescription|() as text)
lns3.scpt
if not incFolders then
lns3.scpt
set theURLs to my filesAndPackagesInURLArray:theURLs
lns3.scpt
else if not incFiles then
lns3.scpt
set theURLs to my foldersInURLArray:theURLs
lns3.scpt
return my convertURLs:theURLs resultType:resType
lns3.scpt
end contentsOf:includeInvisibles:includeFiles:includeFolders:resultType:
lns3.scpt
on entireContentsOf:aFileOrPath includeInvisibles:incInvis includeFiles:incFiles includeFolders:incFolders resultType:resType
lns3.scpt
set theOptions to 2 -- NSDirectoryEnumerationSkipsPackageDescendants
lns3.scpt
if not incInvis then set theOptions to theOptions + 4 -- NSDirectoryEnumerationSkipsHiddenFiles
lns3.scpt
set theEnumerator to NSFileManager's |defaultManager|()'s enumeratorAtURL:theURL includingPropertiesForKeys:{} options:theOptions errorHandler:(missing value)
lns3.scpt
set theURLs to theEnumerator's allObjects()
lns3.scpt
end entireContentsOf:includeInvisibles:includeFiles:includeFolders:resultType:
lns3.scpt
on sortFilesOrPaths:listOfFilesOrPaths byProperty:theProp lowToHigh:ascendFlag resultType:resType
lns3.scpt
set theCodes to {"mod", "add", "cre", "las"}
lns3.scpt
set theProperties to {NSURLContentModificationDateKey, NSURLAddedToDirectoryDateKey, NSURLCreationDateKey, NSURLContentAccessDateKey}
lns3.scpt
set chosenProp to NSURLContentModificationDateKey -- default
lns3.scpt
repeat with i from 1 to count of theCodes
lns3.scpt
if theProp begins with item i of theCodes then
lns3.scpt
set chosenProp to item i of theProperties
lns3.scpt
set allProps to {chosenProp, NSURLPathKey, NSURLNameKey}
lns3.scpt
set containsURLs to ((NSArray's arrayWithArray:listOfFilesOrPaths)'s firstObject()'s isKindOfClass:(|NSURL|)) as boolean
lns3.scpt
set includeURLs to (resType = "files" or resType = "urls")
lns3.scpt
set theResults to NSMutableArray's array()
lns3.scpt
repeat with aFile in listOfFilesOrPaths
lns3.scpt
if containsURLs then
lns3.scpt
set theValues to (aFile's resourceValuesForKeys:allProps |error|:(missing value))
lns3.scpt
if includeURLs then
lns3.scpt
set theValues to theValues's mutableCopy()
lns3.scpt
(theValues's setObject:aFile forKey:"theURLKey")
lns3.scpt
set theURL to (my makeURLFromFileOrPath:aFile)
lns3.scpt
set theValues to (theURL's resourceValuesForKeys:allProps |error|:(missing value))
lns3.scpt
(theValues's setObject:theURL forKey:"theURLKey")
lns3.scpt
(theResults's addObject:theValues)
lns3.scpt
set sortDesc to NSSortDescriptor's sortDescriptorWithKey:chosenProp ascending:ascendFlag selector:"compare:"
lns3.scpt
set sortDesc2 to NSSortDescriptor's sortDescriptorWithKey:NSURLNameKey ascending:true selector:"caseInsensitiveCompare:"
lns3.scpt
theResults's sortUsingDescriptors:{sortDesc, sortDesc2}
lns3.scpt
if resType = "names" then
lns3.scpt
return ((((theResults's valueForKey:NSURLNameKey)'s componentsJoinedByString:(character id 0))'s stringByReplacingOccurrencesOfString:":" withString:"/")'s componentsSeparatedByString:(character id 0)) as list
lns3.scpt
else if resType = "POSIX names" then
lns3.scpt
return (theResults's valueForKey:NSURLNameKey) as list
lns3.scpt
else if (resType = "files" and AppleScript's version > "2.4") then
lns3.scpt
return (theResults's valueForKey:"theURLKey") as list
lns3.scpt
else if resType = "urls" then
lns3.scpt
return theResults's valueForKey:"theURLKey"
lns3.scpt
return (theResults's valueForKey:NSURLPathKey) as list
lns3.scpt
end sortFilesOrPaths:byProperty:lowToHigh:resultType:
lns3.scpt
on makeURLFromFileOrPath:theFileOrPathInput
lns3.scpt
set theFileOrPath to (NSArray's arrayWithObject:theFileOrPathInput)'s firstObject()
lns3.scpt
if (theFileOrPath's isKindOfClass:(NSString)) as boolean then
lns3.scpt
if (theFileOrPath's hasPrefix:"/") as boolean then -- full POSIX path
lns3.scpt
return |NSURL|'s fileURLWithPath:theFileOrPath
lns3.scpt
else if (theFileOrPath's hasPrefix:"~") as boolean then -- POSIX path needing ~ expansion
lns3.scpt
return |NSURL|'s fileURLWithPath:(theFileOrPath's |stringByExpandingTildeInPath|())
lns3.scpt
else -- must be HFS path
lns3.scpt
return |NSURL|'s fileURLWithPath:(POSIX path of theFileOrPathInput)
lns3.scpt
else if (theFileOrPath's isKindOfClass:(|NSURL|)) as boolean then -- happens with files and aliases in 10.11
lns3.scpt
return theFileOrPath
lns3.scpt
else -- must be a file or alias
lns3.scpt
end makeURLFromFileOrPath:
lns3.scpt
on copyFromURL:sourceURL toURL:destinationURL withReplacing:replaceFlag
lns3.scpt
set theResult to destinationURL's checkResourceIsReachableAndReturnError:(missing value)
lns3.scpt
if theResult as boolean is false then
lns3.scpt
set {theResult, theError} to (NSFileManager's |defaultManager|()'s copyItemAtURL:sourceURL toURL:destinationURL |error|:(reference))
lns3.scpt
if not replaceFlag then return false
lns3.scpt
my atomicReplaceFromURL:sourceURL toURL:destinationURL
lns3.scpt
end copyFromURL:toURL:withReplacing:
lns3.scpt
on moveFromURL:sourceURL toURL:destinationURL withReplacing:replaceFlag
lns3.scpt
set {theResult, theError} to (theFileManager's moveItemAtURL:sourceURL toURL:destinationURL |error|:(reference))
lns3.scpt
theFileManager's removeItemAtURL:sourceURL |error|:(missing value)
lns3.scpt
end moveFromURL:toURL:withReplacing:
lns3.scpt
on copyURL:theSourceURL uniquelyToFolderURL:theDestFolderURL
lns3.scpt
set theDestURL to theDestFolderURL's URLByAppendingPathComponent:destName
lns3.scpt
set theExtension to destName's |pathExtension|()
lns3.scpt
set theResult to theDestURL's checkResourceIsReachableAndReturnError:(missing value)
lns3.scpt
set {theResult, theError} to (theFileManager's copyItemAtURL:theSourceURL toURL:theDestURL |error|:(reference))
lns3.scpt
if i = 1 then
lns3.scpt
set theDestURL to theDestFolderURL's URLByAppendingPathComponent:proposedName
lns3.scpt
end copyURL:uniquelyToFolderURL:
lns3.scpt
on filesAndPackagesInURLArray:theURLs
lns3.scpt
set itemURLs to NSMutableArray's array()
lns3.scpt
repeat with aURL in theURLs -- is it a directory?
lns3.scpt
if theValue as boolean then -- is it a package?
lns3.scpt
(itemURLs's addObject:aURL)
lns3.scpt
return itemURLs
lns3.scpt
end filesAndPackagesInURLArray:
lns3.scpt
on foldersInURLArray:theURLs
lns3.scpt
if not theValue as boolean then
lns3.scpt
end foldersInURLArray:
lns3.scpt
on convertURLs:theURLs resultType:resType
lns3.scpt
return ((((theURLs's valueForKey:"lastPathComponent")'s componentsJoinedByString:(character id 0))'s stringByReplacingOccurrencesOfString:":" withString:"/")'s componentsSeparatedByString:(character id 0)) as list
lns3.scpt
return (theURLs's valueForKey:"lastPathComponent") as list
lns3.scpt
return theURLs
lns3.scpt
else if resType = "files" and AppleScript's version > "2.4" then
lns3.scpt
return theURLs as list
lns3.scpt
return (theURLs's valueForKey:"path") as list
lns3.scpt
end convertURLs:resultType:
lns3.scpt
on stringFromRestype:resType
lns3.scpt