Only continue Automator script when Applescript is done - applescript

I'm coding a small Automator script with an Applescript in it.
The Applescript runs a shell script that downloads some images to a specified folder.
The next step in the Automator script makes a PDF from those images.
Small problem, the creation of the PDF already starts before the downloading of the images is done, so i get an empty PDF file.
how can i let Automator wait until the Applescript is done downloading before it gets to the next step?

What shell command(s) are you using to do the download? curl? One way to do this would be to add something to the shell script after each download, like:
curl http://whatever/Your/Url/is/file1.xxx;echo ''>file1.deleteme
then make sure your applescript only starts the pdf part after it finds the file1.deleteme file and deletes it. When I do this with curl, it will not make that empty file until it is done downloading.
the AS part could start with something like (danger, not tested):
--within outer loop for each file...
tell application "Finder"
repeat until exists (pathToDeleteMeFile)--this is built using basename + "deleteme"
delay 1--one second delay
end repeat
end tell
--do pdf stuff here for that (each) file
or, if you prefer (or if this makes more sense depending on what your code looks like:
do the shell script for all files, create a more generic deleteme file after they are all done, then do the pdf stuff.

Automator does wait until Run Script actions are done. The problem, at a guess without seeing your code, is that your AppleScript is using some sort of asynchronous download method -- telling Safari, perhaps? -- so the script finishes before the download is actually done. Using a synchronous method, such as the above suggestion of curl(1), should fix things.
By the way, using a Run Script action to call do shell script is a bit silly; you could just use Run Shell Script instead.

Related

Set Extended Attributes with AppleScript Objc

So I am looking for a way to set Mac specific extended attributes (specifically kMDItemWhereFroms) for a file (Image File, Jpg) using AppleScript or AppleScript-Objc.
There is a command line tool that will do this xattr -w kMDItemWhereFroms . The problem is that on the several machines that I have access to (10.12, 10.13, and 10.14) when you run this command as a do shell script from within an AppleScript it does not work, the metadata is not added to the file. If I set Script Debugger to debug mode, and go through the script step by step it will actually set the metadata, but since that is not the way I am running the script, it is more of an interesting fluke than anything else. I have tried running the command with both "com.apple.metadata:" included and not included with the shell script and that makes no difference.
I have tried running my script through SD, Script Editor and osascript, and they all fail to update the metadata. So I am thinking that this tool might be broken when called from an AppleScript.
I found setxattr but that looks like it only applies to C.
So my questions are
1. Is there a way to set the extended attributes of a file on MacOS using Aobjc? if not then
2. Is there a way to get setxattr to work with either version of AppleScript? Probably not so
3. Any ideas how I might be able to get the command line tool xattr -w kMDItemWhereFroms to work when using scripting?
This is more of an annoyance for me, I am just being stubborn with wanting the source of the file to show up in the "Where From" data in the Get Info window from the Finder. I already am setting some metadata for the file using exiftool. So it is more of an interesting problem for me, than a critical problem that I must try and solve now. Thanks!

Automatic encryption/decryption: detect file is closed in Mate/Gnome application

I'm writing a bash script to automatically decrypt file for editing and encrypt it back after file is closed. File type could be any: plain-text, office document, etc. I am on Linux Mint with Mate.
I'm stuck: can't reliably detect if file was closed in application so that script can proceed to encrypting it back and removing decrypted version.
The first version of the script simply used vim with text files. Script was calling it directly and hadn't been going any further until vim was closed. Now as I want to be able to do so with other files, I tried the following things:
xdg-open: exits immediately after calling the application associated with file type. Thus script continues and does no good.
xdg-open's modified function for calling an associated app: runs it inside the current script so now I see program exit. Works only if the application hasn't been running already. If it has, then new process is finished and script continues.
So what I am trying to do now is to watch somehow that file was closed in already running application. Currently experimenting with pluma/gedit and inotifywait. It doesn't work either - instantly after file was opened it detects CLOSE_NOWRITE,CLOSE event.
Is it at all possible to detect this without specific hooks for different applications? Possibly some X hooks?
Thank you.
You could use lsof to determine if a file is opened by a process:
myFile="/home/myUser/myFile"
/usr/sbin/lsof "$myFile" | grep "$myFile"
You can use a 1 second loop and wait until the lsof response is empty. I have used this to help prevent a script from opening a newly discovered file that is still being written or downloaded.
Not all processes hold a file open while they are using it. For example, vim holds open a temporary file (/home/myUser/.myFile.swp) and may only open the real file when loading or saving.
You might do something like this.
decrypt "TheFile"&
pluma "TheFile"
encrypt "TheFile"
The & at the end of a line will execute the line then fall through to Pluma. The script will pause until pluma closes.
I could offer more help if you post your script.

How to create an executable command prompt script

I usually perform actions in the 7zip command line program. I was thinking about creating a small script to do everything automatically, but I have never written any Windows shell script before and therefore don't even know where to begin.
I would like to make an executable script file which, when a user double-clicks on it, will call the 7zip command line and perform some actions.
First of all, is this possible? And if it is, what is the best way to do this?
You can create a batch script to do this.
It's basically command line commands that run one after another so you don't have to keep typing them in :)
Put the commands you would normally use for 7zip in a notepad file and save it with the extension .bat, then run it.
7z blah blah params
7z more params and args
All your commands will be executed automatically when the previous one finishes.
There are other programming languages you could do this in (or even VBScript) but batch would be perfectly suited to this, and you don't need to install anything extra.
Batch files can run a series of command line commands. Simply create a text file and name it with the .bat extension.
There are plenty of resources on the internet which will provide you with help.

How can I run a shell script on only the next startup? Is it possible to set this up from within a script?

I am trying to make a script to run Once on next startup in OSX Lion. The script is very simple, I am just recording the time of the boot and comparing it to a previous know time. What I would really like to do though is to not have to manually add a startup item, since this will be set up on a very large number of computers.
Is there a way to add a startup item from a script to only run once?
Say your script is called script.sh... Place the script in the directory /System/Library/StartupItems/
Then, just simple add the command rm -f /System/Library/StartupItems/script.sh to the end of your script.

Need to write an applescript to do a series of wget commands for downloading images from a list

I have wget installed on my Mac and it works great. I'm trying to automate the process of pulling thousands of images by using an applescript to do it, however, I'm missing something in the formatting.
How can I make an applescript that will send a wget, wait til the file is downloaded, then issue the next wget command, etc. all in the same terminal window?
Thanks in advance.
set downloads to {"http://google.com/", "http://example.com/"}
tell application "Terminal"
activate
repeat with page in downloads
do script "wget " & page in tab 1 of window 1
end repeat
end tell

Resources