runprogram

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

Re: runprogram

Post by Piero »

Shardik wrote: Thu Jan 23, 2025 7:34 pmI still hope to find an easy alternative
What about putting com.fantaisiesoftware.purebasicide on the plist of the PB's compiled app, grant perms, delete it, and see what happens?
I'm kidding; that's probably far more risky than the sqlite stuff :lol:

PS: talking about this, very strange permission stuff can happen if you duplicate an applescript app and then modify it; it's always better to paste the source code on a brand new Script Debugger doc…

PPS: maybe that can happen also for "duplicate/debug temporary copies" of PB apps (with same bundle id) :shock:

Apple… :?

Note: Это сообщение может не подойти для AZJIO
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: runprogram

Post by Shardik »

Shardik wrote: Thu Jan 23, 2025 7:34 pm I still hope to find an easy alternative with Apple Script and will report about it as soon as I have been successful.
The following PureBasic program executes an AppleScript to open the calendar. In order to be allowed to open the calendar at the requested date ("current date"), an allowance requester is displayed for the PureBasic IDE. With this alternative (instead of using the IDE of PB 5.73 as already described in a previous posting) it is possible to trigger the allowance requester for accessing the calendar also in MacOS Sonoma and Sequoia. I tested it successfully with PB 6.12 and 6.20 Beta 3 (Asm and C backend).

Code: Select all

EnableExplicit

#AppleScript = "" +
  ~"tell application \"Calendar\"" + #CR$ +
  ~"  view calendar at current date" + #CR$ +
  ~"end tell"

Define AppleScriptFile.S
Define ExitCode.I
Define Output.S
Define ProgramID.I
Define Msg.S

AppleScriptFile = GetTemporaryDirectory() + "OpenCalendar.scpt"

If OpenFile(0, AppleScriptFile) = 0
  MessageRequester("Error", "Failed to save AppleScript file!") 
  End
EndIf

WriteString(0, #AppleScript)
CloseFile(0)

ProgramID = RunProgram("osascript", AppleScriptFile, "",
  #PB_Program_Open | #PB_Program_Read | #PB_Program_Error)

If ProgramID
  WaitProgram(ProgramID)
  Msg = ReadProgramString(ProgramID)

  If Msg <> ""
    Output + Msg
    Output + #CR$ + #CR$
  EndIf

  Msg = ReadProgramError(ProgramID)

  If Msg <> ""
    Output + "Error: " + Msg
    Output + #CR$ + #CR$
  EndIf

  ExitCode = ProgramExitCode(ProgramID)
  DeleteFile(AppleScriptFile)

  If ExitCode <> 0
    Output + "Exit code: " + ExitCode
    CloseProgram(ProgramID)
    MessageRequester("Error in AppleScript", Output)
  EndIf
EndIf
User avatar
Piero
Addict
Addict
Posts: 865
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: runprogram

Post by Piero »

Shardik wrote:The following PureBasic program executes an AppleScript
Nice, Thanks!
Does that work using a shell?
If so, mksoft will get angry because you "wasted" a file for an applescript (old story…)! :mrgreen:

Edit: simplified shell version (no error checking); it "works" (asks for perms) on my mac:

Code: Select all

Procedure simpleShell(ShellCommand$)
   Protected 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

simpleShell(~"osascript -e 'tell app \"Calendar\"\nview calendar at current date\nend tell'")
Edit 2: Contacts

Code: Select all

simpleShell(~"osascript -e 'tell app \"Contacts\" to set thePerson to first person'")
Last edited by Piero on Thu Jan 30, 2025 4:20 pm, edited 1 time in total.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: runprogram

Post by Shardik »

Piero,

I have tested your calendar example in PB 6.12 and 6.20 Beta 4 and can confirm that it's also working in MacOS 16.3 'Sonoma'. It triggers the system permission requester and after confirmation it adds a PureBasic IDE entry to
System settings > Privacy & Security > Calendar

Your are right that your example is even simpler than my example. But my example will report an exact AppleScript error message if there is any error in the AppleScript or other things went wrong.

Another alternative to execute an AppleScript and display error infos would be mk-soft's solution.
User avatar
Piero
Addict
Addict
Posts: 865
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: runprogram

Post by Piero »

Shardik wrote: Thu Jan 30, 2025 11:27 amBut my example will report an exact AppleScript error message if there is any error in the AppleScript or other things went wrong.

Another alternative to execute an AppleScript and display error infos would be mk-soft's solution.
It's easy to manage errors with the shell method (see my "shell" link above if you may be interested; I've just updated it, so maybe you will have to scroll toward bottom…)

I tried to find applescript callings for microphone etc., not easy…
I wonder: being that the shell method is runprogram-calling shell+osascript, maybe runprogram-calling (with connect?) an app that needs mic permissions will do the trick…
User avatar
Piero
Addict
Addict
Posts: 865
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Update

Post by Piero »

I was able to get mic permission request (for Terminal) by running the binary of audio recording apps in Terminal

Code: Select all

/Applications/OBS.app/Contents/MacOS/OBS
With PB, via runprogram+shell (/bin/zsh) or directly via runprogram it didn't work, but dunno in Sonoma or PB beta…

More generally, seems that if it opens in Terminal, it quits immediately in PB runprogram, and vice versa :shock:

PS/Edit: strangely, PB console acts like runprogram, even if it uses Terminal :shock: :shock:

PS/Edit2: programs not producing stdout/stderr
/Applications/ocenaudio.app/Contents/MacOS/ocenaudio (did not request perms to me)
seem to always work in Terminal, PB runprogram and PB console mode

Edit 3:
runprogram with |#PB_Program_Connect in console mode will open programs like terminal does, but it will be terminal asking for perms :cry: :cry:
Post Reply