mxrclxst
mxrclxst

Reputation: 142

Is there a way to get the full path of a file in AppleScript?

Using POSIX path returns a string that looks like this:

"/Volumes/Parent Folder/File.fl"

but I need a full path that looks like this:

"smb://something.domain.com/Parent Folder/File.fl"

Is there a way to do this in applescript? I need to be able to get the network sources or the info for the mounted volume so that I can generate URLs for people to click.

Upvotes: 2

Views: 2019

Answers (2)

user3439894
user3439894

Reputation: 7555

Since you haven't provided the output of the command in my comment, here is an example that you should find useful:

This was actually tested from macOS High Sierra to a SMB share on a macOS Catalina and posixFileName is the actual POSIX path to the file from macOS High Sierra. So the uncommented do shell script command actually worked in real world testing.

--  # The value of posixFileName is statically assigned for demonstration purposes.
--  # It's assumed it will be assigned dynamically in the actually working script.

set posixFileName to "/Volumes/Temp Folder/Some Stuff/foobar.txt"

--  # Use AppleScript's text item delimiters to parse posixFileName 
--  # to posixParentFolderName and posixChildFolderAndOrFileName.
--  #
--  # E.g. posixParentFolderName of posixFileName is: '/Volumes/Temp Folder'
--  # E.g. posixChildFolderAndOrFileName of posixFileName is: 'Some Stuff/foobar.txt'
--  #

set AppleScript's text item delimiters to "/"
set posixParentFolderName to text items 1 thru 3 of posixFileName as string
set posixChildFolderAndOrFileName to text items 4 thru -1 of posixFileName as string
set AppleScript's text item delimiters to ""

--  # Uncomment the next line of code for the working script, but use the 
--  # sample further below just to test the example posixFileName above.

-- set mountOutput to do shell script "mount | grep '" & posixParentFolderName & "'; exit 0"

--  # This is just sample output for demonstration purposes and not used when
--  # the above line is uncommented and then this line of code gets removed.

set mountOutput to "//[email protected]/Temp%20Folder on /Volumes/Temp Folder (smbfs, nodev, nosuid, mounted by me)"


if mountOutput is not equal to "" then
    
    --   # Use AppleScript's text item delimiters to parse mountOutput
    --   # to get e.g. '192.168.2.103/Temp%20Folder'
    
    set AppleScript's text item delimiters to " on "
    set serverPathname to first text item of mountOutput
    set AppleScript's text item delimiters to "@"
    set serverPathname to second text item of serverPathname
    set AppleScript's text item delimiters to ""
    
    --  # Combine elements to get the SMB URL.
    
    set smbPathname to "smb://" & serverPathname & "/" & posixChildFolderAndOrFileName
    
    --  # Replace any spaces ' ' with '%20'.
    
    set AppleScript's text item delimiters to " "
    set smbPathname to text items of smbPathname
    set AppleScript's text item delimiters to "%20"
    set smbPathname to smbPathname as string
    set AppleScript's text item delimiters to ""
    
    return smbPathname
    --  # Result: "smb://192.168.2.103/Temp%20Folder/Some%20Stuff/foobar.txt"
else
    return
end if

Upvotes: 2

JMichaelTX
JMichaelTX

Reputation: 1817

Question:

Is there a way to get the full path of a file in AppleScript?
I need to be able to get the network sources or the info for the mounted volume so that I can generate URLs for people to click

Since there is some ambiguity in your question, here is an AppleScript that provides all references to a file selected in the Finder.app.

tell application "Finder"
    set {oFile} to selection
    set fileAlias to oFile as alias
    set posixPath to POSIX path of fileAlias
    set fileURL to URL of oFile
end tell

Upvotes: 1

Related Questions