Viewing *.PNG, *.PDF and *.JPG on MacBook with m1

Mac OSX specific forum
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Viewing *.PNG, *.PDF and *.JPG on MacBook with m1

Post by Piero »

Code: Select all

Procedure simpleShell(ShellCommand$)
   Protected shell
   shell = RunProgram("/bin/sh","","", #PB_Program_Open|#PB_Program_Write)
   If shell
      WriteProgramStringN(shell,ShellCommand$)
      WriteProgramData(shell,#PB_Program_Eof,0)
      CloseProgram(shell)
   EndIf
EndProcedure

PV.s = ~"\"Preview\""

; CLOSE FRONTMOST WINDOW
simpleShell("osascript -e 'tell app " + PV + " to close window 1'")

; QUIT
simpleShell("osascript -e 'tell app " + PV + " to quit'")
User avatar
Lord
Addict
Addict
Posts: 907
Joined: Tue May 26, 2009 2:11 pm

Re: Viewing *.PNG, *.PDF and *.JPG on MacBook with m1

Post by Lord »

Hi Piero!

Thank you for looking into the problem.
Your simpleShell() doesn't work as wanted/expected.

Runnig the procedure with "to close window 1" doesn't close
the just opened "preview"-window. Subsequent opening next
picture(s) opens another "preview" and so on.

Running "to close window 1" followed by " to quit" doesn't
show any "preview"-window.
Image
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 460
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Viewing *.PNG, *.PDF and *.JPG on MacBook with m1

Post by Mindphazer »

Something like this ?

Code: Select all

Procedure simpleShell(ShellCommand$)
   Protected shell
   shell = RunProgram("/bin/sh","","", #PB_Program_Open|#PB_Program_Write)
   If shell
      WriteProgramStringN(shell,ShellCommand$)
      WriteProgramData(shell,#PB_Program_Eof,0)
      CloseProgram(shell)
   EndIf
EndProcedure

PV.s = ~"\"Preview\""
File.s = GetUserDirectory(#PB_Directory_Documents) + "test.pdf"

If OpenWindow(0, 0, 0, 222, 200, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  ButtonGadget(0, 10,  10, 200, 20, "Open Preview with PDF")
  ButtonGadget(1, 10,  40, 200, 20, "Quit Preview")
  ButtonGadget(2, 10,  70, 200, 20, "Close Preview Window")
  Repeat
    Event = WaitWindowEvent()
    EventGadget = EventGadget()
    If Event = #PB_Event_Gadget
      Select EventGadget
        Case 0
          Pgm = RunProgram("open", "-a " + PV + " " + File, "")
        Case 1
          simpleShell("osascript -e 'tell app " + PV + " to quit'")
        Case 2
        simpleShell("osascript -e 'tell app " + PV + " to close window 1'")
      EndSelect
    EndIf
  Until Event = #PB_Event_CloseWindow
EndIf
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Viewing *.PNG, *.PDF and *.JPG on MacBook with m1

Post by Piero »

Lord wrote: Fri Dec 22, 2023 12:37 pm looking into the problem
Check if PB (for testing) and your app have Automation permission for Preview in system prefs/security/privacy/automation,
because osx sometimes fails to show the dialog and you have to do it manually...

use close window 1 only if you want to keep Preview open while your app is running...
User avatar
Lord
Addict
Addict
Posts: 907
Joined: Tue May 26, 2009 2:11 pm

Re: Viewing *.PNG, *.PDF and *.JPG on MacBook with m1

Post by Lord »

Hi Mindphazer & Piero!

Thanks to both of you for trying to solve this (my) little problem.
Again, it does not work here.

Code: Select all

Static pgm
...
...
  file=#DQUOTE$+file+#DQUOTE$
  PV.s = ~"\"Preview\""
  If pgm
    simpleShell("osascript -e 'tell app " + PV + " to quit'")
    simpleShell("osascript -e 'tell app " + PV + " to close window 1'")
    pgm=0
  EndIf
  pgm = RunProgram("open", "-a " + PV + " " + File, "")
No matter, in wich order I execute "to quit" and / or "to close window 1"
Either each call opens a new window and they stay or first picture is
shown and the next call closes the window and the next picture and
all following pics are not shown.

OK, thanks for help, but I'm out of this. Time is running out and there's
still a lot to do. I'm complete new on MacBook and this isn't even mine.
Image
Post Reply