p7zip and runprogram

Mac OSX specific forum
pierre2
New User
New User
Posts: 7
Joined: Fri Feb 21, 2020 4:40 pm

p7zip and runprogram

Post by pierre2 »

Apologies for another RunProgram question. I am trying to use p7zip (executable name: 7za) via RunProgram.

Having navigated to the correct directory, the following works in Terminal:

7za a MyBackup *.pdf

(this copies and compresses all .pdf files in the directory, and saves them in an archive called MyBackup, in the same directory)

The following doesn't work, and I have tried dozens of permutations, including without the Chr(34)s, from the various posts in the forum on the subject.

Code: Select all

theSubDirs.s = "~/Documents/BusRecs testing/"
...
Case #PB_Event_Gadget
If EventGadget() = #theButton
  SetCurrentDirectory(theSubDirs)
  theApp.s = "7za a MyBackup *.pdf"

  If RunProgram("open", Chr(34) + theApp + Chr(34), "")
    MessageRequester("Test", "OK")
  Else
    MessageRequester("Test", "Problem")
  EndIf

EndIf
It says "OK", but does not create the archive.

Any help please ? Many thanks.
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: p7zip and runprogram

Post by Marc56us »

RunProgram <> 0 mean launching program OK (program or document exist) but does not mean all parameters OK

Some things to test:

Add #PB_Program_Wait to see console information (errors).

I think this is a path problem, I don't know if unix shell shortcut for home dir (~) can be use in a program ?
Try GetHomeDirectory() + "BusRecs testing/" instead

Add -bb3 (to 7zip parameters) to see log
"show information about additional operations (Analyze, Replicate) for "Add" / "Update" operations."

PS. I don't have a Mac. This may be helpfull ?
viewtopic.php?p=548226#p548226

:wink:
pierre2
New User
New User
Posts: 7
Joined: Fri Feb 21, 2020 4:40 pm

Re: p7zip and runprogram

Post by pierre2 »

Thanks for your ideas, and for your time. I tried all of your suggestions, including all variations of the Mac post (except, it seems, the correct one !), e.g.

Code: Select all

theApp.s = "-a 7za --args " + Chr(34) + "a MyBackup *.pdf" + Chr(34)
- no luck yet ...
pierre2
New User
New User
Posts: 7
Joined: Fri Feb 21, 2020 4:40 pm

Re: p7zip and runprogram

Post by pierre2 »

Making progress with a combination of zip (rather than p7zip), a shell script, and of course PB. The p7zip version of the shell script works from the command line, but I can't get it working from RunProgram, so I am now using zip instead.
User avatar
deseven
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: p7zip and runprogram

Post by deseven »

7za is not a default system utility, it's located in a directory that is not included in a default PATH, you can't call it without full path to it.
Zip is a default system utility, you can call it just fine by its name.
Oh, and you can't use ~ anywhere you want, RunProgram() (or SetCurrentDirectory()) is not a shell, it won't expand anything for you.

The correct way to do what you want:

Code: Select all

RunProgram("/usr/local/bin/7za","a MyBackup *.pdf","/path/to/my/cool/directory")
If you really need ~, expand it manually:

Code: Select all

RunProgram("/usr/local/bin/7za","a MyBackup *.pdf",GetEnvironmentVariable("HOME") + "/my/cool/directory")
But that's useless since PB's packer module fully supports both 7z and zip archives and can pack and unpack anything you want without using external tools. Look into UseLZMAPacker() and UseZipPacker().
pierre2
New User
New User
Posts: 7
Joined: Fri Feb 21, 2020 4:40 pm

Re: p7zip and runprogram

Post by pierre2 »

Stunning - thank you so much for your reply, which explains so much. I had no idea that PB had 7zip functionality built in. I have stashed your entire reply away in my file of "really useful stuff".
User avatar
Piero
Enthusiast
Enthusiast
Posts: 346
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: p7zip and runprogram

Post by Piero »

deseven wrote: Tue Mar 10, 2020 2:18 amIf you really need ~, expand it manually:

Code: Select all

RunProgram("/usr/local/bin/7za","a MyBackup *.pdf",GetEnvironmentVariable("HOME") + "/my/cool/directory")
Won't work: the "*"(.pdf) would be interpreted 'literally' Image
:P
User avatar
deseven
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: p7zip and runprogram

Post by deseven »

Piero wrote: Wed Aug 09, 2023 8:41 pm Won't work: the "*"(.pdf) would be interpreted 'literally' Image
Ah, right, it's also shell functionality.
You could try something like that:

Code: Select all

RunProgram("/bin/sh",~"-c \"/usr/local/bin/7za a MyBackup *.pdf\"",GetEnvironmentVariable("HOME") + "/my/cool/directory")
Explanation: spawn a copy of /bin/sh that will expand *.pdf into the list of files and run 7za with it.
User avatar
Piero
Enthusiast
Enthusiast
Posts: 346
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: p7zip and runprogram

Post by Piero »

deseven wrote: Thu Aug 10, 2023 9:01 am spawn a copy of /bin/sh
Thanks, that can be useful and gives some possibilities (even pipe thru other commands…)
I tested workarounds like that; the BIG problem is when you need double quotes in your shell command…
User avatar
deseven
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: p7zip and runprogram

Post by deseven »

Piero wrote: Thu Aug 10, 2023 2:44 pm the BIG problem is when you need double quotes in your shell command…
Not a problem if you pipe your command into the shell instead of passing it as an argument (standard shell quote rules do apply, though):

Code: Select all

shell = RunProgram("/bin/sh","",GetEnvironmentVariable("HOME") + "/my/cool/directory",#PB_Program_Open|#PB_Program_Write)
If shell
  WriteProgramStringN(shell,"/usr/local/bin/7za a MyBackup *.pdf")
  WriteProgramData(shell,#PB_Program_Eof,0)
  While ProgramRunning(shell) : Delay(10) : Wend ; comment out if you don't need to wait for completion
  CloseProgram(shell)
EndIf
User avatar
Piero
Enthusiast
Enthusiast
Posts: 346
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: p7zip and runprogram

Post by Piero »

deseven wrote: Thu Aug 10, 2023 2:54 pm Not a problem
Many, many, many thanks! That will be very useful to me!
Post Reply