Need Help Getting a Shell Script to Work in KM

This is not related to KM directly, but if anyone has an idea, can you help? I am trying to execute terminal code from an Applescript that looks like this

do shell script** ("mv " & temp_folder & "* " & destination_folder_pg & thetitle & "") as string

which generates code like this

mv /Users/pp/posthere/* '/Users/pp/Documents/location/the file Peter is trying to download.png'

This code works fine when copied to the clipboard and pasted into Terminal but when run from Applescript it gives an error and shows this

Screen Shot 2021-06-03 at 17.04.45

I tried referencing mv as /bin/mv/ since that's it's actual location but that didn't make the AS version work. Any ideas about what would make a simple move command fail when run from terminal through Applescript?

Well that doesn’t look right for a start.

It's not in the original, it gets added when I paste certain characters here.

That's doesn't, at first sight, look like an output string generated by the script itself.

With code like that (preparing strings for a shell command) accidents can be reduced by writing something like:

do shell script strCommand

where strCommand is a string which:

  1. is separately assembled, and
  2. can be separately inspected before sending it to the shell.

If you can:

  1. make that adjustment
  2. and paste the string actually generated by your script here

between triple backticks, to prevent the wiki software from modifying it

```
-- code here
```

then we may be able to get some insight.

Hey Peter,

Firstly let me put my moderator hat on and ask that you use proper technique when posting code or any text that must be protected from being rendered by HTML.

How to Post Code (Scripts) on the KM Forum; and Macro to Paste AppleScript in a Code Block

It limits the possibility of mangled code/data and saves everyone time and aggravation.

Also – when posting code please try to post a test case that's complete enough to run on the tester's machine.

To test your code above we have to make assumptions.


Now – to your problem...

Never piece together shell code in AppleScript and execute it on the same line. Use a construct like this:

# This works out of the box.
set temp_folder to "~/'test_directory/mv_source'/"
set destination_folder_pg to "~/'test_directory/mv_destination'/"
set thetitle to "Peter Test File.txt"

set shCmdStr to "mv " & temp_folder & "* " & destination_folder_pg & quoted form of thetitle
do shell script shCmdStr

This way you can comment-out the do shell script line, run the script, and eyeball the shell code prior to execution.

Note where I've single-quoted my paths above. This is not necessary with this script, because there are no spaces in those paths, BUT it's a good habit to get into – because spaces WILL bite you sooner or later.

In fact I think they did bite you in your script above. I think it's balking because you probably have spaces in your file name.


Next question: Why are you using the shell to move your file when you're already in AppleScript?

--------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2021/06/03 06:23
# dMod: 2021/06/03 06:23 
# Appl: Finder
# Task: Move a File from a Target Dir to a Destination Dir.
#     : Rename the file if necessary.
# Libs: None
# Osax: None
# Tags: @cctone, @Applescript, @Script, @ASObjC, @Finder, @Move, @File, @Rename.
--------------------------------------------------------

use AppleScript version "2.4" --Β» Yosemite or later
use framework "Foundation"
use scripting additions

set tempFolderPath to "~/test_directory/mv_source/"
set destinationFolderPath to "~/test_directory/mv_destination/"
set fileName to "Peter Test File.txt"

set tempFolderPath to alias POSIX file (((current application's NSString's stringWithString:tempFolderPath)'s stringByExpandingTildeInPath) as text)
set destinationFolderPath to alias POSIX file (((current application's NSString's stringWithString:destinationFolderPath)'s stringByExpandingTildeInPath) as text)

tell application "Finder"
   set fileToMoveRef to first item of tempFolderPath as alias
   set movedFileRef to move fileToMoveRef to destinationFolderPath
   if name of movedFileRef is not fileName then set name of movedFileRef to fileName
end tell

--------------------------------------------------------

Next question: Do you need to be in AppleScript at all? Can you just use a Execute a Shell Script action and keep everything in the shell?

Or – can you use Keyboard Maestro's own Move or Rename a File action?

-Chris

1 Like

Thanks very much for the reply. I will make note of the notation to use in the future. I got it working with the Finder command!

I think the reason I wasn't getting success with move and rename was that I needed to move the file that was in a certain location (whose name I didn't know) and wasn't finding a way to select * in the filename in KM.

Here's some more insanity though. I am trying to get the frame rate of a gif using ffprobe so I can convert the gif to an MP4 (required for posting to social media using Buffer), at the correct frame rate. When I run this code in Terminal

/usr/local/bin/ffprobe /Users/pp/Desktop/file_test.gif

it shows a wall of data including metadata, duration, bitrate and so on. My "endgame" is to append a | grep (or cut, or awk) and just find the code '25 fps' (or whatever number is before fps) so I can capture the right number I need.

My frustration is the above command is another mysterious case of a command that works perfectly in Terminal not return any data if run through Applescript and "do shell script." I tried it in KM also, and it also returns no data...not an error, just no reaction at all. Any idea what I'm doing wrong?

Thanks in advance for anything you can suggest!

I thought, if it's going to refuse to return output for this command, I'll write it out to a file! This command works fine, writing the data I want out to a file

/usr/local/bin/ffprobe /Users/pp/Desktop/file_test.gif |& tee ~/documents/output.txt

but when run from Applescript the existence of the & character causes an error, and nothing can get around it.

do shell script "/usr/local/bin/ffprobe /Users/pp/Desktop/file_test.gif |& tee ~/documents/output.txt" 

The & causes an error for running directly in KM as well.

Perhaps worth defining "nothing" ?

What have you tried ?

Hey Peter,

ffprobe looks to be one of those commands that does not buffer its output and expects to write line by line to a Terminal, so flat output like in the KM Execute a Shell Script action doesn't work.

You can get around this by redirecting STDERR to STDOUT.

Execute a Shell Script.kmactions (728 B)

Not knowing what you're doing is such a PITA sometimes...

This is a hard problem to solve even after considerable research. I spent many hours solving a similar problem years ago, so I'm reasonably familiar with the issues (although not expert to be sure).

To get this into AppleScript you can do something like this:

set shCmdStr to "
export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH;
ffprobe ~/Documents/Catch_All/Yacht.jpg 2>&1 | sed -n 'p' > ~/Downloads/zzz.ffprobe.report.txt
"
do shell script shCmdStr

-Chris

THANK YOU. I can't believe how much time I spent on this. This works great, though, creating the output so I can parse it easily for my script. Thanks again!

1 Like

Tell me about it...

Sometimes things that seem like they should be dead simple turn out to be the most unbelievable cans of worms.

This is why I advise people to not spend more than half an hour banging their head against the wall – there's too much dain brammage in the world already.

It's different when you're making progress – even really slow progress can be edifying and satisfying – but when you're well and truly stuck it's time to ask for help.

:sunglasses:

-Chris

3 Likes

Thanks again. I use FFMPEG to convert GIFs to MP4s, and you saved me from having weirdly fast-or-slow anime GIFs on social media.

1 Like