简体   繁体   中英

In Applescript: Copy the POSIX path of a selected file to the clipboard

If I select a file in the finder on my local or a remote server, I want the script to paste the POSIX path to the clipboard as text, for pasting in a chat window, etc.

This is what I have so far but it is not very clean:

on run
    tell application "Finder"
        copy selection to theSelected
        set outputPathList to {}
        set anItem to theSelected
        copy (POSIX path of (anItem as alias)) to end of outputPathList

        set AppleScript's text item delimiters to return
        set outputString to outputPathList as string
        set AppleScript's text item delimiters to ""

        set the clipboard to outputString
    end tell
end run

Any ideas on how to clean this up?

on run
    tell application "Finder"
        set theItem to selection as string
    end tell
    set posixForm to POSIX path of theItem
    set the clipboard to posixForm
end run

The property selection of the Finder returns a list of Finder specifiers.
This script copies all POSIX paths (one per line) to the clipboard.

tell application "Finder" to set theSelection to (get selection)
if theSelection is {} then return
set outputPathList to {}
repeat with anItem in theSelection
    set end of outputPathList to POSIX path of (anItem as text)
end repeat
set {TID, text item delimiters} to {text item delimiters, return}
set the clipboard to outputPathList as text
set text item delimiters to TID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM