Page 1 of 1

Debug or "Save" .app

Posted: Mon Sep 22, 2025 10:09 am
by Piero

Code: Select all

-- Debug or "Save" your created app, without affecting the rest of its package contents!
-- Save this AppleScript as app. Do not use "purebasic." (with a dot) in your PB app name beginning
-- Drag it into a folder containing your PB app and the PB "compile/run" temporary app (…and your source…) 
-- There must be NO other apps! (set PB prefs to "create temporary executable in source folder")
-- Run it and "Replace & Debug"! (It will ask for permissions; be sure to grant!)
-- You can open MULTIPLE DEBUG SESSIONS by clicking "Just Debug"
-- NOTE: You can use it to create the executable only once, and then always use compile/run, with or WITHOUT Debugger to "Save"
property deb : "/Applications/PureBasic.app/Contents/Resources/compilers/pbdebugger.app/Contents/MacOS/pbdebugger " -- add space
property pb : "purebasic." -- PB temp name begins with this
try
	-- tell application "System Events" to set visible of application process "PureBasic" to false -- hide PB
	tell application "Finder"
		set dir to container of (path to me)
		set nf to name of (path to me)
		set myapp to 1st item of dir whose name ends with ".app" and name is not nf and name does not start with pb
		set pbapp to 1st item of dir whose name ends with ".app" and name is not nf and name begins with pb
		set nmyapp to characters 1 thru -5 of (name of myapp as text) as text
		set npbapp to characters 1 thru -5 of (name of pbapp as text) as text
		set pbapp to POSIX path of (pbapp as text) & "/Contents/MacOS/" & npbapp
		set myapp to POSIX path of (myapp as text) & "/Contents/MacOS/" & nmyapp
		set cp to "cp -f " & quoted form of pbapp & " " & quoted form of myapp
		set dlb to button returned of (display dialog "Copy —> Replace:" & linefeed & linefeed & pbapp & "\n\n" & myapp ¬
			with title "Replace " & nmyapp & "?" buttons {"Replace & Debug", "Just Debug", "Just Replace"} default button 1)
		if dlb contains "Replace" then do shell script cp -- copy/replace executable
		if dlb contains "Debug" then
			do shell script deb & quoted form of myapp & " > /dev/null 2>&1 &" -- open with debugger
			delay 0.3
			tell application "System Events" to set frontmost of last application process whose name is "pbdebugger" to true
			tell application "System Events" to set frontmost of last application process whose name is nmyapp to true
		end if
	end tell
on error e
	display dialog e buttons "OOOPS!" with title "Error!" default button 1
end try

Re: Debug or "Save" .app

Posted: Mon Sep 22, 2025 1:59 pm
by Piero
Example test app:

Code: Select all

If FindString(GetFilePart(ProgramFilename()),"PureBasic.")=1 : End : EndIf ; just compile on compile/run

Debug ProgramFilename()
MessageRequester("Test","Test")

Re: Debug or "Save" .app

Posted: Mon Sep 22, 2025 3:19 pm
by Piero
Test app 2

Code: Select all

#TestDeb = #True ; set to #False when done with testing and debugging…
CompilerIf #TestDeb
   Macro TestDeb
      #AppleScriptApp="DebugApp.app"
      
      Procedure.s UpperDir(path$, level = 1, setcurrdir = #False)
         If FileSize(path$) = -1 : ProcedureReturn "" : EndIf
         Protected i, OLDcurrdir$ = GetCurrentDirectory()
         path$ = GetPathPart(path$) ; in case of file
         For i = 1 To level : path$ + ".." + #PS$ : Next
         SetCurrentDirectory(path$)
         path$ = GetCurrentDirectory()
         If Not setcurrdir : SetCurrentDirectory(OLDcurrdir$) : EndIf
         ProcedureReturn path$
      EndProcedure
      
      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
      
      Procedure.s QuoteString(str$) ; useful to quote FILE/FOLDER PATHS
         ProcedureReturn "'" + ReplaceString(str$, "'", "'\''") + "'"
      EndProcedure
      
      If FindString(GetFilePart(ProgramFilename()),"PureBasic.")=1
         simpleShell("open "+QuoteString(UpperDir(ProgramFilename(),3)+#AppleScriptApp))
         End ; Just open #AppleScriptApp and quit
      EndIf
   EndMacro
CompilerElse
   Macro TestDeb : EndMacro
CompilerEndIf

TestDeb

; You will probably want to always just compile…
If FindString(GetFilePart(ProgramFilename()),"PureBasic.")=1 : End : EndIf ; just compile on compile/run

Debug ProgramFilename()
MessageRequester("Test","Test")