text
stringlengths
0
15.7k
source
stringlengths
6
112
_support's throwCommandError("File", handlerName, eText, eNumber, eFrom, eTo)
lns3.scpt
script _NSStringEncodings
lns3.scpt
set _list to {¬ {UTF8 encoding, current application's NSUTF8StringEncoding}, ¬
lns3.scpt
{UTF16 encoding, current application's NSUTF16StringEncoding}, ¬
lns3.scpt
{UTF16BE encoding, current application's NSUTF16BigEndianStringEncoding}, ¬
lns3.scpt
{UTF16LE encoding, current application's NSUTF16LittleEndianStringEncoding}, ¬
lns3.scpt
{UTF32 encoding, current application's NSUTF32StringEncoding}, ¬
lns3.scpt
{UTF32BE encoding, current application's NSUTF32BigEndianStringEncoding}, ¬
lns3.scpt
{UTF32LE encoding, current application's NSUTF32LittleEndianStringEncoding}, ¬
lns3.scpt
{ASCII encoding, current application's NSASCIIStringEncoding}, ¬
lns3.scpt
{ISO2022JP encoding, current application's NSISO2022JPStringEncoding}, ¬
lns3.scpt
{ISOLatin1 encoding, current application's NSISOLatin1StringEncoding}, ¬
lns3.scpt
{ISOLatin2 encoding, current application's NSISOLatin2StringEncoding}, ¬
lns3.scpt
{JapaneseEUC encoding, current application's NSJapaneseEUCStringEncoding}, ¬
lns3.scpt
{MacOSRoman encoding, current application's NSMacOSRomanStringEncoding}, ¬
lns3.scpt
{NonLossyASCII encoding, current application's NSNonLossyASCIIStringEncoding}, ¬
lns3.scpt
{ShiftJIS encoding, current application's NSShiftJISStringEncoding}, ¬
lns3.scpt
{Symbol encoding, current application's NSSymbolStringEncoding}, ¬
lns3.scpt
{WindowsCP1250 encoding, current application's NSWindowsCP1250StringEncoding}, ¬
lns3.scpt
{WindowsCP1251 encoding, current application's NSWindowsCP1251StringEncoding}, ¬
lns3.scpt
{WindowsCP1252 encoding, current application's NSWindowsCP1252StringEncoding}, ¬
lns3.scpt
{WindowsCP1253 encoding, current application's NSWindowsCP1253StringEncoding}, ¬
lns3.scpt
{WindowsCP1254 encoding, current application's NSWindowsCP1254StringEncoding}}
lns3.scpt
set textEncoding to textEncoding as constant
lns3.scpt
if aRef's item 1 is textEncoding then return aRef's item 2
lns3.scpt
on error number -1700 -- not a predefined constant, but hedge bets as it might be a raw NSStringEncoding number
lns3.scpt
return textEncoding as integer
lns3.scpt
on error number -1700 -- fall through
lns3.scpt
_support's throwInvalidConstantParameter(textEncoding, "using")
lns3.scpt
to read from file theFile as dataType : (text) using textEncoding : (UTF8 encoding)
lns3.scpt
set posixPath to _support's asPOSIXPathParameter(theFile, "")
lns3.scpt
set dataType to _support's asTypeParameter(dataType, "as")
lns3.scpt
if dataType is text and textEncoding is not primary encoding then -- note: AS treats `text`, `string`, and `Unicode text` as synonyms when comparing for equality, which is a little bit problematic as StdAdds' `read` command treats `string` as 'primary encoding' and `Unicode text` as UTF16; passing `primary encoding` for `using` parameter provides an 'out'
lns3.scpt
set textEncoding to _NSStringEncodings's getEncoding(textEncoding)
lns3.scpt
set {asocString, theError} to current application's NSString's stringWithContentsOfFile:posixPath encoding:textEncoding |error|:(specifier)
lns3.scpt
if asocString is missing value then error (theError's localizedDescription() as text) number theError's code() from theFile to dataType
lns3.scpt
set fh to open for access (posixPath as POSIX file)
lns3.scpt
set theResult to read fh as dataType
lns3.scpt
close access fh
lns3.scpt
_error("read file", eText, eNumber, eFrom, eTo)
lns3.scpt
end read from file
lns3.scpt
to write to file theFile data theData as dataType : (text) using textEncoding : (UTF8 encoding)
lns3.scpt
if dataType is text and textEncoding is not primary encoding then
lns3.scpt
set asocString to current application's NSString's stringWithString:(_support's asTextParameter(theData, "data"))
lns3.scpt
set {didSucceed, theError} to asocString's writeToFile:posixPath atomically:true encoding:textEncoding |error|:(specifier)
lns3.scpt
if not didSucceed then
lns3.scpt
error (theError's localizedDescription() as text) number theError's code() from theFile to dataType
lns3.scpt
set fh to open for access (posixPath as POSIX file) with write permission
lns3.scpt
set eof fh to 0 -- important: when overwriting an existing file, make sure its previous contents are erased first
lns3.scpt
write theData to fh as dataType
lns3.scpt
_error("write file", eText, eNumber, eFrom, eTo)
lns3.scpt
end write to file
lns3.scpt
to convert path filePath from fromFormat : (POSIX path format) to toFormat : (POSIX path format) -- brings a modicum of sanity to the horrible mess that is AppleScript's file path formats and file identifier types
lns3.scpt
if (count {filePath} each text) = 0 then -- assume it's a file identifier object (alias, «class furl», etc)
lns3.scpt
set posixPath to _support's asPOSIXPathParameter(filePath, "")
lns3.scpt
else -- it's a text path in the user-specified format, so convert it to a standard POSIX path
lns3.scpt
if fromFormat is POSIX path format then
lns3.scpt
set posixPath to filePath
lns3.scpt
else if fromFormat is HFS path format then -- caution: HFS path format is flawed and deprecated everywhere else in OS X (unlike POSIX path format, it can't distinguish between two volumes with the same name), but is still used by AS and a few older scriptable apps so must be supported
lns3.scpt
set posixPath to POSIX path of (file filePath)
lns3.scpt
else if fromFormat is file URL format then
lns3.scpt
set asocURL to current application's |NSURL|'s URLWithString:filePath
lns3.scpt
if asocURL is missing value or not asocURL's fileURL() then _support's throwInvalidParameter(filePath, "", text, "Not a file URL.")
lns3.scpt
_support's throwInvalidConstantParameter(fromFormat, "from")
lns3.scpt
if posixPath's length = 0 then _support's throwInvalidParameter(filePath, "", text, "Path can’t be empty.")
lns3.scpt
if toFormat is POSIX path format then
lns3.scpt
return posixPath
lns3.scpt
else if toFormat is alias file object then -- returns object of type `alias`
lns3.scpt
return posixPath as POSIX file as alias
lns3.scpt
else if toFormat is POSIX file object then -- returns object of type `«class furl»`
lns3.scpt
return posixPath as POSIX file
lns3.scpt
else if toFormat is file specifier object then -- returns an _object specifier_ of type 'file'. Caution: unlike alias and «class furl» objects, this is not a true object but may be used by some applications; not to be confused with the deprecated `file specifier` type («class fss»), although it uses the same `file TEXT` constructor. Furthermore, it uses an HFS path string so suffers the same problems as HFS paths. Also, being a specifier, requires disambiguation when used [e.g.] in an `open` command otherwise command will be dispatched to it instead of target app, e.g. `tell app "TextEdit" to open {fileSpecifierObject}`. Horribly nasty, brittle, and confusing mis-feature, in other words, but supported (though not encouraged) as an option here for sake of compatibility as there's usually some scriptable app or other API in AS that will absolutely refuse to accept anything else.
lns3.scpt
return a reference to file (posixPath as POSIX file as text) of AppleScript
lns3.scpt
else if toFormat is HFS path format then
lns3.scpt
return posixPath as POSIX file as text
lns3.scpt
else if toFormat is file URL format then
lns3.scpt
set asocURL to current application's |NSURL|'s fileURLWithPath:posixPath
lns3.scpt
if asocURL is missing value then _support's throwInvalidParameter(filePath, "", text "Can’t convert to file URL.")
lns3.scpt
return (asocURL's absoluteString()) as text
lns3.scpt
_support's throwInvalidConstantParameter(toFormat, "to")
lns3.scpt
_error("convert path", eText, eNumber, eFrom, eTo)
lns3.scpt
end convert path
lns3.scpt
to normalize path filePath tilde expansion expandingTilde : (false) ¬
lns3.scpt
absolute expansion expandingRelative : (false) link expansion expandingSymlink : (false)
lns3.scpt
set filePath to _support's asPOSIXPathParameter(filePath, "")
lns3.scpt
if not _support's asBooleanParameter(expandingTilde, "tilde expansion") ¬
lns3.scpt
and filePath starts with "~" then set filePath to "./" & filePath -- Cocoa API *always* expands leading "~" character, which it really shouldn't as tilde expansion is a *nix shell-ism, not a POSIX path-ism, so prefix with "./" to prevent that
lns3.scpt
if _support's asBooleanParameter(expandingRelative, "absolute expansion") ¬
lns3.scpt
and filePath does not start with "/" then set filePath to join path {current working directory, filePath}
lns3.scpt
if _support's asBooleanParameter(expandingSymlink, "alias expansion") then
lns3.scpt
return ((current application's NSString's stringWithString:filePath)'s stringByResolvingSymlinksInPath()) as text
lns3.scpt
return ((current application's NSString's stringWithString:filePath)'s stringByStandardizingPath()) as text
lns3.scpt
_error("normalize path", eText, eNumber, filePath, eTo)
lns3.scpt
end normalize path
lns3.scpt
to join path pathComponents using file extension fileExtension : ("")
lns3.scpt
set subPaths to items of _support's asListParameter(pathComponents, "")
lns3.scpt
if subPaths is {} then error
lns3.scpt
repeat with aRef in subPaths
lns3.scpt
set aRef's contents to _support's asPOSIXPathParameter(aRef's contents, "")
lns3.scpt
_support's throwInvalidParameter(pathComponents, "", list, "Expected one or more text and/or file items.")
lns3.scpt