문제

I would like to create a TextExpander snippet that will prompt me to choose a file in the Finder and copy the file path to the text editor I am working in.

It should work like this:

  1. I am working in NVAlt
  2. I enter the TextExpander command (e.g. ;path)
  3. I am prompt top choose a file
  4. The file path is copied to the note I am working in.

I figured I'll do it using AppleScript, so I tried this:

set source_folder to choose file with prompt "Please select a file."
tell application "System Events"
   set item_list to POSIX path of every disk item of source_folder
end tell
return "path:" & item_list

But it is not returning anything to the text note I am working in...

Any suggestion how I should do it?

PS: I used the POSIX command to the folder name as ".../.../.../" instead of "...:...:..."

도움이 되었습니까?

해결책

There are multiple things wrong in the script but instead of trying to correct your script I've looked what textExpander tells me to do. On the help section about textExpander on smilesoftware.com site they say:

The script executes in the context of the TextExpander application. The script can perform various actions, but the snippet will expand to whatever text is returned.

When looking at the your goal you simply need to coerce the returned alias from the choose file command into a string and return that. So a single line like below should be enough to use to return an HFS path to a file

return choose file with prompt "Please select a file." as string

if you want to return the file path as POSIX Path you only need this:

return POSIX path of (choose file with prompt "Please select a file.")

If you want the path to be prefixed with "path:" like in your example code:

return "path:" & (choose file with prompt "Please select a file.")

NOTE: there is an implicit coerce of the choose file results, no explicit coercion is needed.

EDIT: I've downloaded textExpander myself and it seems that showing dialog in the context of textExpander will behave wrong. So what I did was looking up the front most application and showing the dialog there, it's also better looking. Then I tell that application explicitly to show the choose file prompt and return the result of the choose file prompt. Here is the code:

tell application "System Events"
    set applicationName to (name of every process whose frontmost is true) as string
end tell

using terms from application "AppleScript Editor"
    tell application applicationName
        set expansionString to (choose file "Please select a file.") as string
    end tell
end using terms from

return expansionString
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top