0

I have an Automator service using a shell script that I'm executing using a keyboard shortcut to copy file/folder pathnames to the clipboard. The service receives selected files or folders in Finder app, passes them as arguments to the following shell script.

for f in ”$@”
do
echo \""$f"\"
# also tried it with "\"$f\""
done

This outputs to a copy to clipboard action. It works fine when just pasting into a text editor, Finder's search box, etc.

However, I also have a conditional script running through QuicKeys that allows me to paste text in certain problematic text fields of various windows in an audio app called REAPER (otherwise, the shortcut for pasting text, ⌘+v, simply doesn't work). The weird thing is, if I copy a pathname from Finder, paste it into a text editor, select it, and copy it, then the script to paste the copied text works fine in REAPER. But going straight from Finder, copying the pathname to the clipboard, then trying to paste it in a text field in REAPER doesn't work.

Here's the applescript I'm calling with the QuicKeys shortcut.

global frontApp, frontAppName, windowTitle, seltxt
set windowTitle to ""

tell application "System Events"
    set frontApp to first application process whose frontmost is true
    set frontAppName to name of frontApp
    tell process frontAppName
        tell (first window whose value of attribute "AXMain" is true)
            set windowTitle to value of attribute "AXTitle"
        end tell
    end tell
end tell

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

set RprRunning to is_running("REAPER")

try
    if RprRunning then
        tell application "System Events"
            if exists (window "Save" of process "REAPER") then
                tell text field "Save As:" of window "Save" of application process "REAPER" to set seltxt to the value of its attribute "AXSelectedText"
                tell text field "Save As:" of window "Save" of application process "REAPER" to set the value of its attribute "AXSelectedText" to the clipboard
                
            else if exists text field 6 of window windowTitle of application process "REAPER" then
                tell sixth text field of window windowTitle of application process "REAPER" to set seltxt to the value of its attribute "AXSelectedText"
                tell sixth text field of window windowTitle of application process "REAPER" to set the value of its attribute "AXSelectedText" to the clipboard
                
            else if exists (text field 2 of window windowTitle of application process "REAPER") then
                tell second text field of window windowTitle of application process "REAPER" to set seltxt to the value of its attribute "AXSelectedText"
                tell second text field of window windowTitle of application process "REAPER" to set the value of its attribute "AXSelectedText" to the clipboard
                
            else if exists (text field 1 of window windowTitle of application process "REAPER") then
                tell first text field of window windowTitle of application process "REAPER" to set seltxt to the value of its attribute "AXSelectedText"
                tell first text field of window windowTitle of application process "REAPER" to set the value of its attribute "AXSelectedText" to the clipboard
    
            end if
        end tell
        return seltxt
    end if
end try

I'd really love to get this working properly. I've been racking my brain trying to figure out why copied file/folder pathnames need to be pasted & copied in some text field outside of REAPER before they can be pasted into a text field in REAPER.

Max Well
  • 257
  • 2
  • 9
  • 1
    What about: `printf '%s\n' "${f//\"/\"\"}"` – Jetchisel Oct 28 '22 at 16:00
  • 1
    In `”$@”`, you have "smart quotes"; they need to be `"$@"` -- plain ASCII quotes. – Charles Duffy Oct 28 '22 at 16:02
  • (without understanding _why_ you're writing `echo \""$f"\"`, I can't speak to whether it's right or wrong; _generally_ one should only be using syntactic and not literal quotes in a well-written shell script) – Charles Duffy Oct 28 '22 at 16:03
  • Also, REAPER has its own very powerful Python, Lua, and EEL (JavaScript-like) APIs. If you use those instead of AppleScript your code will be shareable with REAPER users on other non-Apple platforms. https://www.reaper.fm/sdk/reascript/reascript.php is a good place to start. – Charles Duffy Oct 28 '22 at 16:03
  • @CharlesDuffy Thanks for the info! unfortunately, this problem can't be solved within reaper because of the fact that certain plugins, and UI prompts of the rescript API itself, hijack the shortcuts for copy paste actions. Believe me, it's a problem I've been struggling to solve for several years. It's something in regards to the way the application manages window indexing and focus. thus far, the only workaround seems to be using a script running outside of reaper, bound to the same shortcuts when the application is frontmost. That's where QuicKeys comes in. – Max Well Oct 29 '22 at 00:21
  • @CharlesDuffy I checked my script and it doesn't have smart quotes for `"$@"`. I'm not sure how they got in there in my post. I guess it must've been whichever method I used to copy/paste the script into my browser that somehow converted them. – Max Well Oct 29 '22 at 21:27
  • @Jetchisel how is the printf script you wrote supposed to work? I've been trying it in my shell script in Automator, but it keeps failing with an error. – Max Well Oct 29 '22 at 22:04
  • @MaxWell, I have no idea about Automator but you change `printf` to `echo` , other than that, I can't help. – Jetchisel Oct 29 '22 at 22:08

1 Answers1

0

Well, I'm not sure if there might be something to fix still with one of the scripts. But for now, I seem to have stumbled upon a solution.

Rather than piping the output from the shell script directly to the copy to clipboard action, I added a filter paragraphs action before it, and set it to return paragraphs that: begin with: "

This seems to have had the same effect as pasting and copying the path name string in a text field outside of REAPER, and I'm now able to paste the pathnames directly into text fields in reaper after copying them from selected files/folders in Finder.

Max Well
  • 257
  • 2
  • 9
  • 1
    Can't test your workflow but if you select a file in the Finder and copy to the clipboard, you aren't merely copying the filename (as text) to the clipboard. You're also copying a file (in multiple ways). After doing the above, open up the 'clipboard viewer' app and look at all the stuff you've collected. Then, open an empty, plain TextEdit doc and paste. Then copy the pasted text and look again in the viewer. You'll see a much different result. I would guess that the clipboard doesn't know what Reaper expects and tries giving it the wrong thing. – Mockman Oct 29 '22 at 02:41
  • @Mockman Yes this is what I was thinking was the case. Despite following instructions online to create an Automator workflow to run as a service to copy the pathnames to the clipboard, they obviously missed the step of stripping away all other data besides the text string for the path. So I suppose, unless there's some way to do this completely in shell/Apple script (maybe utilizing `printf '%s\n' "${f//\"/\"\"}"` as @Jetchisel suggested), I think my solution using the Automator service & QuicKeys+Applescript will suffice, despite the lack of centralization. – Max Well Oct 29 '22 at 09:41
  • 1
    The way to get pristine text is to collect it from a source that disallows non-text, e.g. a browser's address bar, the text of a file's name when clicking on (and highlighting) the name, so as to be able to edit it. A file however, has other uses (e.g. for when you drag it somewhere) and therefore, comes with additional formats. If you click on a file's name and then copy and review in the Clipboard Viewer, you'll see only two formats. Reaper is an issue because it isn't scriptable and doesn't provide any insight as to which data to provide when you paste. Try ' – Mockman Oct 29 '22 at 16:14
  • 1
    That said, you (probably) don't need pristine text as you can reduce the variations of the clipboard by using 'as text'. Put this somewhere at the top of your script (it must run and before the try statements): `set clipVar to the clipboard as text set the clipboard to clipVar`. Note, the above is two lines. I should add that there is no simple way to strip away all other data so it's not a failure in the procedures you followed. – Mockman Oct 29 '22 at 16:20