Macro/script to put POSIX path of file on the clipboard

I'm looking for a script and/or macro to call the file chooser (file open dialogue box) and choose a file. The path of the file should be placed on the clipboard.

I've found this at flipmartin.net:

tell application "System Events"
activate
set f to choose file
set p to POSIX path of f
do shell script "open " & quoted form of p
end tell

This opens the nice dialogue box with Favourites etc. that I want.

How can I change the line with "do" to place the chosen file on the clipboard, instead of opening it with the assigned app?

Or is there a better way, in KBM? Without the script, I mean...

Thanks!

Hans

1 Like

It could be done like this:

1 Like

Hey Guys,

<choose file>

Has nothing to do with System Events; it’s a part of the Standard Additions OSAX. The reason for using SEV is to provide a context for the choose file dialog to open. I prefer to use the Keyboard Maestro Engine for this, since it’s already running.

Note also that this script restores the previous app’s context, instead of invisibly leaving you hanging in System Events.

set frontApp to path to frontmost application as text
tell application "Keyboard Maestro Engine"
  activate
  set fileList to choose file with multiple selections allowed
  repeat with i in fileList
    set contents of i to POSIX path of (contents of i)
  end repeat
  set AppleScript's text item delimiters to linefeed
  set fileList to fileList as text
  set the clipboard to fileList
end tell
tell application frontApp to activate

This script allows one or more files to be chosen from the dialog.

The syntax also allows for a few more options as well:

set theResult to choose file with prompt text ¬
  of type list of text default location alias ¬
  invisibles boolean ¬
  multiple selections allowed boolean ¬
  showing package contents boolean


Best Regards,
Chris