osx - MacScript Invalid procedure call or argument -
in osx excel provides functionality run applescript code via macscript keyword. running shell code via functionality , looks this:
macscript "do shell script ""/usr/bin/whatever""" recently decided wanted capture output (and stderr) because command failing , wanted see error was... rewrote this:
macscript "do shell script ""/usr/bin/whatever > /tmp/out 2>&1""" but error above. if run given command in applescript editor, error:
the command exited non-zero status (number: 1)
so question is: how trap return code of macscript , prevent excel breaking? tried:
dim rc integer: rc = macscript(...) but script still breaks!
generally, prevent do shell script (and therefore, indirectly, macscript()) throwing error, make sure shell command exits code 0.
in case, merely capture shell command's exit code, append ; echo $? command string passed do shell script:
simplified example, using malformed date command:
dim rc integer rc = macscript("do shell script ""date -nosuchoptions; echo $?""") ' returns 1 echo $?outputs (preceding) command's exit code stdout , therefore returneddo shell script- as - desirable - side effect, exit code set
0, becauseechocommand succeeds; therefore, overall command exits code0, preventingdo shell scriptthrowing error.
caveats:
- since you're assigning
macscript's return valueintegervariable, suredo shell scriptcommand's output can parsed number.
(the above command safe, yours question, because preceding command's stdout output captured in file, , subsequentecho $?guaranteed output "number-looking" string.) - if shell command syntactically incorrect,
macscriptstill throw error; use distinguish between syntax , runtime errors.
if, contrast, still want return command's output , know in abstract whether went wrong:
dim stdout string on error resume next ' ignore runtime errors stdout = macscript("do shell script ""date -nosuchoptions""") if err.number <> 0 msgbox "something went wrong.", vbexclamation else msgbox "captured output: " & stdout, vbinformation end if - note method not allow determine shell command's specific exit code, because excel vba translates any nonzero shell exit code generic error number
5(invalid procedure call or argument).
finally, can combine 2 approaches - return command's output and specific exit code:
dim stdout string, rc integer, pos integer ' execute command, , appends exit code *last line* of output. ' note how both stdout , stderr output captured via `2>&1`. stdout = macscript("do shell script ""{ date; date -nosuchoptions; } 2>&1; echo $?""") ' extract last line output captured. pos = instrrev(stdout, chr$(13)) ' find last line break (\n has been translated \r `do shell script`) rc = mid(stdout, pos + 1) # exit code stdout = left(stdout, pos - 1) # captured output (only) if rc <> 0 msgbox "command failed exit code " & rc & "; captured output: " & stdout, vbexclamation else msgbox "captured output: " & stdout, vbinformation end if caveats:
- if shell command syntactically incorrect,
macscriptstill throw error; use distinguish between syntax , runtime errors. - if shell command (the 1 preceding
echo $?) doesn't terminate output\n, parsing out exit code not work.
Comments
Post a Comment