Extract file extension: Difference between revisions

Added AppleScript solutions.
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
(Added AppleScript solutions.)
Line 229:
/etc/pam.d/login -> ( ) as expected
</pre>
 
=={{header|AppleScript}}==
AppleScript paths can have either of two formats, depending on the system used to access the items on a disk or network.
The current task specification implies that the slash-separated "POSIX" format is intended in all cases.
Some macOS "files" are actually directories called "bundles" or "packages". Their paths may or may not end with separators.
Underscores are valid extension characters in macOS and extensions are returned without leading dots.
When extracting extensions in AppleScript, one would normally follow the rules for macOS, but variations are possible.
The task specification is taken at its word that the input strings do represent paths to files.
 
===Vanilla===
<lang applescript>on getFileNameExtension from txt given underscores:keepingUnderscores : true, dot:includingDot : false
set astid to AppleScript's text item delimiters
-- Extract the file or bundle name from the path.
set AppleScript's text item delimiters to "/"
if (txt ends with "/") then
set itemName to text item -2 of txt
else
set itemName to text item -1 of txt
end if
-- Extract the extension.
if (itemName contains ".") then
set AppleScript's text item delimiters to "."
set extn to text item -1 of itemName
if ((not keepingUnderscores) and (extn contains "_")) then set extn to ""
if ((includingDot) and (extn > "")) then set extn to "." & extn
else
set extn to ""
end if
set AppleScript's text item delimiters to astid
return extn
end getFileNameExtension
 
set output to {}
repeat with thisString in {"http://example.com/download.tar.gz", "CharacterModel.3DS", ".desktop", "document", "document.txt_backup", "/etc/pam.d/login"}
set end of output to {thisString's contents, getFileNameExtension from thisString with dot without underscores}
end repeat
 
return output</lang>
 
{{output}}
<pre>{{"http://example.com/download.tar.gz", ".gz"}, {"CharacterModel.3DS", ".3DS"}, {".desktop", ".desktop"}, {"document", ""}, {"document.txt_backup", ""}, {"/etc/pam.d/login", ""}}</pre>
 
===ASObjC===
 
AppleScriptObjectiveC makes the task a little easier, but not necessarily more efficient.
 
<lang applescript>use AppleScript version "2.4" -- Mac OS X 10.10 (Yosemite) or later.
use framework "Foundation"
 
on getFileNameExtension from txt given underscores:keepingUnderscores : true, dot:includingDot : false
-- Get an NSString version of the text and extract the 'pathExtension' from that as AppleScript text.
set txt to current application's class "NSString"'s stringWithString:(txt)
set extn to txt's pathExtension() as text
if ((not keepingUnderscores) and (extn contains "_")) then set extn to ""
if ((includingDot) and (extn > "")) then set extn to "." & extn
return extn
end getFileNameExtension
 
set output to {}
repeat with thisString in {"http://example.com/download.tar.gz", "CharacterModel.3DS", ".desktop", "document", "document.txt_backup", "/etc/pam.d/login"}
set end of output to {thisString's contents, getFileNameExtension from thisString with dot without underscores}
end repeat
 
return output</lang>
 
{{output}}
<pre>{{"http://example.com/download.tar.gz", ".gz"}, {"CharacterModel.3DS", ".3DS"}, {".desktop", ".desktop"}, {"document", ""}, {"document.txt_backup", ""}, {"/etc/pam.d/login", ""}}</pre>
 
=={{header|Arturo}}==
557

edits