Automator launching python script - macos

So I'm trying to have a button in my Excel spreadsheet launch a Python script. Apparently this is only possible on Windows, so on the Mac version people have a workaround where they use Excel to launch an AppleScript which then launches a Python script.
In Automator I've tried selecting Application, and when that didn't work I selected Quick Action, and both times I inserted a 'Run Shell Script' module.
Within that I set the Shell to 'usr/bin/python' and Pass input as 'As Arguments'. Then in the text field I put
do shell script "/Users/user_name/Desktop/integrationtest.py"
and I tried
do shell script "python /Users/user_name/Desktop/integrationtest.py"
When I click Run in the top right of Automator it says
The action “Run Shell Script” encountered an error: “ File "<string>", line 1
do shell script "/Users/user_name/Desktop/integrationtest.py"
^
SyntaxError: invalid syntax”
File "<string>", line 1
do shell script "/Users/user_name/Desktop/integrationtest.py"
^
SyntaxError: invalid syntax
Not too sure what I'm doing wrong - any ideas? Thanks for any help

You are mixing up AppleScript and Shell Script syntax.
The Run Shell Script action syntax is
python /Users/user_name/Desktop/integrationtest.py
The Run AppleScript action syntax is
on run {input, parameters}
do shell script "python /Users/user_name/Desktop/integrationtest.py"
return input
end run

I suggest you to go step by step : first open Terminal application and test that your python script runs fine :
python /Users/user_name/Desktop/integrationtest.py
if OK, then open Applescript Editor and create a script with single line : (in Applescript, some environment settings may be different from usual bash Terminal)
do shell script "python /Users/user_name/Desktop/integrationtest.py"
If again this Applescript test works as expected, then call this Applescript from Excel. The method to call depends of your Excel version: With Excel 2011, use Macscript function, and with Excel 2016 use ApplescriptTask.

Related

How to run bash script from macro shell?

How can I create a macro (for instance LibreOffice calc) that runs a bash script in terminal (for instance xfce4-terminal). An example:
#!/bin/bash
echo "Hello world"
echo "Press any key to close this window"
read
I tried the Shell command in macro editor, but nothing happened. Here is what I did:
Sub testMysql
Shell ("/mnt/save/janos/home/testbashsql",4)
end Sub
It compiles and runs without error, but no output. As a side question: what does "compile" mean in this context, i.e. what happens to the compiled code? where is it stored? Why is there a "compile" button?
Thanks for helping me better understand macros.
Calling the script will execute the script in a shell. To see results, the script should write to a file rather than stdout, because LibreOffice does not display stdout.
To open a terminal instead, call the terminal. This worked on my system.
Shell("xterm")
Regarding the compile button in the LO IDE, I use it to check Basic code for any syntax errors. I am not aware of any compiled stored code. Documentation is at https://help.libreoffice.org/Basic/Compile.

Execute a shell command on a file selected in the Finder

I'm a very novice and infrequent applescript experimenter. I've tried for several hours now to learn the individual applescript commands for the following task, but I always run into errors. Perhaps someone much more adept at applescript will find this task easy and quick, and for that I would be very grateful. Here is the task:
I want to be able to manually select a document or file within the finder and then execute the following unix command on that file. I would then store the script under Finder's "Services" menu. The unix command is:
srm -rfv -m path/filename
In my attempts, I assumed that a script that would open Terminal and execute the command would be the way to go, but just couldn't get anything to work. Thank you in advance to any good programmers who can whip out such a script for me.
My tip: Create such services using Automator!
Create a new Service in Automator
Choose "File & Folder" as Input and "Finder"
Add "Run shell script"
Choose "as arguments" as input
Change echo "$f" to your command srm -rfv -m "$f"
Save it as "Safe delete"
From now on, if you select a file inside Finder you will find the option "Safe delete" in the context menu.
Enjoy, Michael / Hamburg
Craig's comment is pertinent, but I am just focus on the script itself, not on the shell command. the script bellow must be saved as Application and each time you drop 1 or more file on its icon, the shell script command will be executed for each file :
on open myFiles
repeat with aFile in myFiles -- repeat loop in case you drop more than 1 file on the icon script
try
do shell script "srm -rfv -m " & (quoted form of POSIX path of aFile)
end try
end repeat
end open
Still make sure that in your shell command 'srm -rfv', the 'v' is necessary because this script will not display any thing ! I don't think so. also I did not display error during remove. what do you want to do with error (like write protect, ...) ?
Update: I missed that the OP wants to create an OS X Service that integrates with Finder. ShooTerKo's answer shows how to do that (and his solution doesn't even require use of AppleScript).
The only potentially interesting thing about this answer is that it demonstrates AppleScript commands to open a file-selection dialog, get the chosen file's POSIX path and run a shell command with it, with some general background information about executing shell commands with do shell script.
Try the following:
# Let the user choose a file via an interactive dialog.
set fileChosen to choose file
# Get the file's POSIX path.
set filePath to POSIX path of fileChosen
# Use the path to synthesize a shell command and execute it.
do shell script "echo srm -rfv -m " & quoted form of filePath
Note:
There's no explicit error handling; if you don't want the script to just fail, you'll have to add error handling (try ... on error ... end try) to handle the case of the user canceling the file selection and the shell command failing (unlikely in this case).
The shell command has echo prepended to it in order to perform a dry run (see its output in Script Editor's Result pane); remove it to perform the actual deletion.
quoted form of is important for ensuring that a string value is included as-is in a shell command (no risk of expansion (interpretation) by the shell).
do shell script will NOT open a Terminal window - it will simply run the shell command hidden and return its stdout output, which is usually what you want. If the shell command signals failure via a non-zero exit code, an error is raised.

Automator: "run shell script" action only receives 1st line of input

I have a weird looking problem (a similar one was asked here, but I don't want to accept that Automator only pipes only 1 line into the shell script action!: Mac Automator: shell script gets only one line)
Automator-Workflow, type "service", 3 blocks:
Service receives "Text"
"run shell script", "bash", input via "stdin", shell script: "cat"
copy to clipboard"
When I select a multiline text and run this service only the first line finishes in the clipboard.
I made three other tests:
skip the shell script action - directly move the selection into the clipboard >> works!
instead of taking the input from the text selection the shell script action gets the input via an "read from clipboard" action from the clipboard >> fails (first line only)
instead of the "bash" action I selected a "perl" action >> fails (first line only)
So it seems obvious that the run shell script action contains the problem.
But I have used the shell script action (with web-content) many times before with no problems.
Any ideas?
Maybe a problem with encoding and or line-endings?
At least on my Mac, when
start Automator
Choose Type -> Service
save at some name (mine is TestService), then
go to TextEdit
enter some text
select
from the TextEdit's menu: TextEdit -> Services -> TestService
Got to the clipboard the next:
2 ééééééééééé
3 íííííííííí
4 αβγδεζη
5 ЧШЩЪЫЬЭ
6 aaaaaaaaaa
Try exactly the above... ;)
I ran into the same issue. Instead of using the 'copy to clipboard' command I then tried executing another shell script (setting its input to 'stdin') containing only the command 'pbcopy'.
Afterwards line breaks were preserved properly.

How to link shared library in shell script?

I have wrote a simple shell script where I have only mentioned the following line
export LD_LIBRARY_PATH=/home/lib/
I want to run one program for which I have to link with this library ,before running the program I am running this shell script ,but after this the program is not working it showing the linking error and when I am doing following line it showing nothing
echo $LD_LIBRARY_PATH
but,when I am doing it in shell normally ,it is working.
Can any one tell why this shell script is not working.what is the concept behind it.
Thanks
If you want to run a script for the purpose of modifying environment variables you need to source the script rather than run the script. Running the script starts a new instance of w/e shell is used to run the script, when it returns, all environment variables are back to the way they were before you ran it. Doing "source script.sh" actually runs the commands in the script in your current shell.

executing terminal commands in Applescript

I executed the terrminal commnand "system_profiler SPApplicationsDataType" in AppleScript for to obtain installed app in MAC, I need to parse the output, Pls explain how to achieve it either thru Apple script or Python.
In Python you can use subprocess -
http://docs.python.org/library/subprocess.html
Specifically, the check_output method: http://docs.python.org/library/subprocess.html#subprocess.check_output
So you would just use:
output = subprocess.check_output(["system_profiler", "SPApplicationsDataType"])
print output # or do something else with it
Your command will be broken into an array, one element for each of your arguments.
In Applescript you could do something like:
set textReturned to (do shell script "system_profiler SPApplicationsDataType")
tell application "TextEdit"
activate
make new document at the front
set the text of the front document to textReturned
end tell
This will run the command and dump the output to TextEdit.

Resources