How to run multiple script files in a sequence on OS X? - macos

I have 10 script files and each file contain up to 100 commands ( each command take 1 min ). At the moment it saves me time and I click only one file to execute 100 commands but what I want is another script file which execute 10 script files sequentially instead of me waiting for each to finish and then executing the second script file by clicking it.

You just call each script as you would before, but seperate them with a semicolon ;
After each script finishes execution, bash will start executing the next script.
In the terminal:
./scriptsrc/script_1; ./scriptsrc/script_2; ./scriptsrc/script_n;
If you need more guidance check this question out, its fairly similar.
EDIT:
If you want to run multiple scripts from one other script this can be accomplished by adding the shebang line to tell the kernel the file is executable and then just listing what scripts you want:
#!/bin/bash
./scriptsrc/script_1
./scriptsrc/script_2
./scriptsrc/script_n
echo "script execution complete"

set a to alias POSIX file "/Users/Firebird/Documents/script1.scpt"
set b to alias POSIX file "/Users/Firebird/Documents/script2.scpt"
set c to alias POSIX file "/Users/Firebird/Documents/script3.scpt"
set theScriptFiles to {a, b, c}
repeat with scriptFile in theScriptFiles
run script scriptFile
end repeat
The last script on the list didn't run the first couple of times when I was testing for an unknown reason, but after I ran
set myList to {"Hello", "Goodbye", "I must be going"}
repeat with theItem in myList
say theItem
end repeat
it worked!
To make sure all the scripts ran I tell Texteditor to open a file for each corresponding script:
tell application "Finder" to open "Macintosh:Users:Firebird:Documents:ranscript1.rtf"
This thread is really good too from MacScripter http://macscripter.net/viewtopic.php?id=44260

Related

How to send a command using AppleScript to terminal one by one and save the output, which is not writable to file anywhere?

So, I have a problem. I have downloaded a program from the web. And it's a command line app. I have written a code, which generated some n-k commands to the app. I have written them into an output file. I can write an app in Python, but it freezes on some of the commands. I have tested them manually and seems like there are two issues:
Commands must be run one-by-one;
Some of the commands give an output like bla-bla-bla, this thing is not written into an output file. So, if I run a command ./app -p /file1 -o /file2 -s -a smth- > /fileOutput.txt The fileOutput.txt is empty, though in the terminal, there's is this bla-bla-bla message, stating, that something is wrong. If the command gives bla-bla-bla the app may freeze for a while.
Here is what I want to do:
CD into folder, the containing app;
For command in fileWithCommands perform command and start the next, only when the previous finishes;
If the command gives message, containing bla-bla-bla (cause it may look like file1 bla-bla-bla), write the command and this strange output into file badOutputs.txt.
Have never done applescript before. However, this's what I've done so far:
set theFile to "/Users/MeUser/Desktop/firstCommand"
set fileHandle to open for access theFile
set arrayCommand to paragraphs of (read fileHandle)
#I have found the previous code here: http://alvinalexander.com/mac-os-x/applescript-read-file-into-list-array-examples
close access fileHandle
tell application "Terminal"
activate
do script "cd /Users/MeUser/Desktop/anApp/"
repeat with command in arrayCommand
do script command
end repeat
end tell
Though there's a problem, if in one window the commands make up a huge queue. Without window 1 cd and the command are in different windows. And I am still unable to save the output.
UPDATE
Did with accordance to #Mark Setchell's recommendations. So now I have such code:
set theFile to "/Users/meUser/Desktop/firstCommand"
set fileHandle to open for access theFile
set arrayCommand to paragraphs of (read fileHandle)
close access fileHandle
repeat with command in arrayCommand
do shell script "cd /Users/meUser/Desktop/App/; " & command
end repeat
To the command I have added the following:
2>&1 /Users/meUser/Desktop/errorOut.txt
However, the apple script says that a mistake of the app is the mistake of the script. I.e.: file corrupted, app fails. I want it to write into error file where has it failed and move to the next command, while the script just fails.
Maybe not a complete solution, but more than a comment and easier to format this way...
First Issue
Your command-line app which writes on the Terminal may be writing to stderr rather than stdout. Try redirecting stderr to the same place as stdout by using
./app -p ... > /FileOutput.txt 2>&1
Second Issue
You cannot do:
do shell script cd somewhere
do shell script do_something
because each do shell script will execute in a separate, unrelated process. So your first process will start - in the default directory like all processes - and correctly change directory and then exit. Then your second process will start - in the default directory like all processes - and try to run your command. Rather than that, you can do this:
do shell script "cd somewhere; do_something"
which starts a single process which changes directory and then runs your command line program there.
Issue Three
Why do you want to send your commands to Terminal anyway? Does the user need to see something in Terminal - seems unlikely because you want to capture the output, don't you? Can't you just run your commands using do shell script?
Issue Four
If you want to keep your normal output separate from your error output, you can do:
./app ... params ... > OutputFile.txt 2> errors.txt
Suggestion 1
You can retain all the errors from all the scripts and accumulate them in a single file like this:
./app .. params .. >> results.txt 2>&1
That may enable you to deal with errors separately later.
Suggestion 2
You can capture the output of your shell script into an Applescript variable, say ScriptOutput, like this, then you can parse it:
set ScriptOutput to do shell script "..."
Suggestion 3
If errors caused by your script are stopping your loop, you can enclose them in a try block like this so they are handled and everything continues:
try
do shell script "..."
on error errMsg
display dialog "ERROR: " & errMsg
end try

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.

Combining two scripts in applescript

I'm a complete newbie to applescript and my new external backup HDD made it necessary for me to work with it. As it is quite noisy I wanted to write a script that mounts the disk (if it is unmounted), runs the backup and then ejects the backup disk again (Code A). So far so good. In order to eject the disk after backup has finished I found a piece of code to check if a process is still running (Code B). It returns 1 if the backup process (backupd) is still alive and 0 if it is finished.
I am struggling now with combining those two pieces. I would like code B to keep checking after the backup has started if backupd is still running and if it is done go to the next step and eject the disk.
I just can't get code B running in code A and also the needed loop confuses me a bit. Any help is really greatly appreciated!! I can't imagine it's that tricky just too much for my imagination Thanks for helping me restoring peace and quietness
Code A:
set myVolumeLabel to "Time Machine"
tell application "Finder"
set diskDev to do shell script "diskutil list | grep \"" & myVolumeLabel & "\" | grep -o > 'disk[0-9]*' "
if not (disk myVolumeLabel exists) then
do shell script "diskutil mountDisk " & diskDev
do shell script "/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-> helper >/dev/null 2>&1 &"
(* Checking if the backupd process is still running should go here I suppose.*)
else
do shell script "diskutil eject /Volumes/'Time Machine' " & diskDev
end if
end tell
Code B
on check_process(marker)
set the_processes to (do shell script "ps -A")
return (the_processes contains marker)
end check_process
if check_process("/backupd") then
set x to "1"
else
set x to "0"
end if
---display dialog x buttons {"OK"} default button 1
Mac OS X (10.6.8)
It seems to me that what you want to do is rather low-level kind of "system stuff" and that more of the code should be done in the shell.
I'm learning both AppleScript and Unix shell-scripting (the bash shell to be precise, which is the default shell in OS X).
Most of your AppleScript here is really mostly shell scripts inside of AppleScript.
It seems like in this case, the right tool for the right job is a shell script.
You may not want to learn a whole 'nother programming language right now, so I'll give you a couple of thoughts.
If you end a shell-command with an ampersand "&" inside of the quotation marks, then AppleScript will NOT wait for the shell script to complete but rather it will return immediately, putting the process on a separate thread and will return a process id.
If you don't terminate a shell-command with an ampersand, then AppleScript will wait for the command to finish before proceeding.
You can try the following experiment. Type in the following command into Terminal.app:
sleep 10
It will take 10 seconds before you get control back in Terminal.
If you type the following command,
sleep 10&
You will get control back immediately and will get a process id back to refer to the process which you have started.
Well, again, it seems to me that the whole script is best written as a bash script, possibly using a little bit of Automator or AppleScript to kick things off.
Mac shell (bash) tutorial:
http://tidbits.com/article/7003
-- Kaydell
Let me be your guide

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.

batch script print the command that would be executed rather than executing

Is it possible to set a cmd.exe shell / batch file to print what would be executed but not actually execute it?
For example, given a batch file that takes some arguments, based on those arguments selects some other batch files to run, those batch files execute some commands, may or may not call other files/commands etc.
I would like to be able to run the top level batch file with all possible combinations of it's input arguments and capture what each arg combination would execute - without actually trying to execute it.
e.g. conceptually would want to be able to produce something like:
mybatchfile.bat 1 2 3 > mybatchfile_1_2_3.bat
mybatchfile.bat 99 3 42 > mybatchfile_99_3_42.bat
where mybatchfile_99_3_42.bat is the list of everything that WOULD be executed when running mybatchfile.bat 99 3 42 (NOT the output of executing those commands)
If this can't be done solely using cmd.exe is there someway to achieve this by running the batch script in cygwin bash shell
In bash we would use something like -x to print out all possible commands without executing them. how to make bash scripts print out every command before executing The problem is that to my knowledge there's no exact equivalent command for Batch Scripts. I would suggest you try placing:
#echo on
at the beginning of your script and:
#echo off
at the end of your script, that's the best starting place.
If you never want the batch file to actually execute the commands, you can insert echo before each command. It's not a perfect solution by any means, but it may be a work-around for fairly simple scripts.

Resources