Skip to content

Instantly share code, notes, and snippets.

@Teraflopst
Last active April 18, 2022 01:48
Show Gist options
  • Save Teraflopst/fd9087b50f8d95697807c6a222dcecd7 to your computer and use it in GitHub Desktop.
Save Teraflopst/fd9087b50f8d95697807c6a222dcecd7 to your computer and use it in GitHub Desktop.
AppleScript: get filename and extension
-- Description
-- Get filename or extension of POSIX path
set p to "/Applications/Safari.app"
set pFile to POSIX file p
tell application "Finder" to set theFullFileName to name of file pFile
-- Get filename "Safari.app" with extension
tell application "Finder" to set theFileExtension to name extension of file pFile
-- Get extension "app"
set xtsLength to count of theFileExtension
-- Get extension "app" length: 3
set theFileName to text 1 thru -(xtsLength + 2) of theFileName
-- Get filename "Safari" without extension
-- Description:
-- Get filepath, filename and extension of selected file
tell application "Finder" to set theFilePath to POSIX path of (selection as alias)
-- "/Applications/Safari.app"
tell application "Finder" to set theFullFileName to name of (selection as alias)
-- "Safari.app"
tell application "Finder" to set theFileExtension to name extension of (selection as alias)
-- "app"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment