Tuesday, November 19, 2019

#1 2019-10-16 07:40:52 pm

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 448

Direct updating shell output to Applescript progress dialog?

I'm guessing the answer here is "You can't do that." But just in case I'm wrong, I thought I'd ask.

Background (why I want this - skip if you want)
I've got a script that needs to upload and download several large files for users. It's using LFTP via do shell script. It works really well.

But:
There are several files, and it would be even faster if it handled these in parallel rather than sequentially. LFTP can handle parallel transfer, but in this case it's not efficient to pass all the files into a single lftp call because the files come from Photoshop doing a modification, then an upload, then a modification, etc. So I don't have all the files available simultaneously to upload. I could save them all out from PS first, but in many cases the Photoshop run time between saves is significant to the upload time, so that doesn't necessarily help much.

So I'd like to spin up a new Applescript process for each file transfer to invoke LFTP again. LFTP can handle this, it can manage a queue and perform the operations in parallel.

So I'm just planning on using Bastiaan Boertien's method here
https://macscripter.net/viewtopic.php?p … 53#p134353
To run a separate script for each, with the calling script passing in the shell line as an argument.

That way Photoshop can do everything as fast as it can without waiting for each upload to finish before going on to the next step, and the last upload can still be happening when the next is queued and they'll just upload in parallel.

I can have each script display a new progress dialog window for it's upload, so the user will be able to see stuff it still going on. But each spun-off Applescript will just be idling while waiting for the "do shell" script to return an Apple Event, so it's just going to be a "barber poll" style dialog with no updates.
However, sometimes the files are big, and sometimes the users are working remotely on slow connections. It isn't absolutely essential, but it would be great if there was some way to keep the users notified what was going on. With a standard Applescript progress dialog with 0 updates, they won't know if an upload hung or is going to take 20 minutes or what.

What I want

LFTP provides great feedback - in Terminal.

So my question is, is there any way to feed the shell output back to an Applescript- live? Not setting a variable to the result of the "do shell script" command and waiting for it to return, but feeding what's being shown in the shell back to Applescript while the user waits, so it could update a progress dialog?

The only ways I could think to do this are:
1. Forget "do shell script" and just actually applescript Terminal to show a shell and run the command... and have the shell script exit, and close the window on a clean shell exit. But people tend to freak out when a terminal window pops up, it's not very UI friendly.

2. Use Platypus or CocoaDialog
https://sveinbjorn.org/platypus
to make the shell script for lftp into an application, that accepts arguments in the $n format for the variables. Although it's unclear to me, if Applescript is calling this as an application, if there's any way to actually pass those variables. But It looks like Platypus or Cocoa Dialog can display what the terminal would be displaying in a UI window.

I'm not thrilled with either of those options, thought I'd ask if there's a better way.

Thanks,

t.spoon

Last edited by t.spoon (2019-10-16 07:41:34 pm)


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#2 2019-10-19 12:29:07 pm

TedW
Member
Registered: 2005-11-02
Posts: 62

Re: Direct updating shell output to Applescript progress dialog?

I don't know Platypus or CocoaDialog, so I'm not sure what your objections to these are. in vanilla AppleScript it's easy enough to detach a shell process and have it write its output to a file, like so:

Applescript:

do shell script "<some LFTP command> &> /path/to/output/file.txt

Adding that '&> ...' tells applescript to stop expecting a response, so that it moves on with subsequent commands.  Then you can set up a polling routine which tests the contents of that output file for changes and notifies the user. That could be as simple as opening the generated file in BBEdit (which will automatically display changes as they come in) or as complex as using AppleScriptObjC to create a new widow and display updated text or some kind of progress bar. Probably you'd want to convert your script into a stay-open application and use an idle handler (delay-repeat loops are clunky), and if you're actually calling the same process and modifying it (as opposed to starting separate instances of LFTP) then you'll probably need to recover the process id so you can refer back.

Long story short, it would be helpful to see your code so I have some idea what you are actually doing. Otherwise I can only talk in generalities like this.

Offline

 

#3 2019-10-21 08:24:26 am

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 448

Re: Direct updating shell output to Applescript progress dialog?

Thanks TedW, that's a fantastic idea I just didn't think of...

have the AppleScript proceed without waiting for shell
log shell stdout to file
have Applescript poll the log file

Great idea, I'll give that a go.

I don't think I even need to go to an ASObjC dialog to update the dialog, I think I can just use Applescript's built-in "progress" features to display the dialog.


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)