PB IDE Tool MyAppData

Mac OSX specific forum
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

PB IDE Tool MyAppData

Post by mk-soft »

Hello

here is a very small code to copy folders and files on-fly into own app

Folder structure:
../[Project-Path]/MyAppData/

Folders inside MyAppData:
./Resources
./Library <- if needed


Update v1.2
- Added: Replace and insert keys and values into 'info.plist'

Update v1.2.01
- Code cleanup

Update v1.2.02
- Change syntax ";PLIST <...>" to ";[Space]PLIST <...>"

Update v1.03.1
- Build with compilermode console

Update v1.04.0
- Added check is Application

Code: Select all

;-TOP
; Tool    : MyAppData - Copied the folder MyAppData in the APP folder \Contents
; Author  : mk-soft
; Version : 1.04.0
; Create  : 10.02.2015
; Update  : 16.01.2022
; 
; Commpilermode : Console  
;
; Info.plist
;
; PLIST <key>CFBundleSignature</key>
; PLIST <string>PB-TOOL</string>
; PLIST <key>CFBundleShortVersionString</key>
; PLIST <string>1.2.0.2</string>
; PLIST <key>CFBundleName</key>
; PLIST <string>PB-Tool MyAppData</string>
; PLIST <key>NSHumanReadableCopyright</key>
; PLIST <string>Copyright © 2022 mk-soft. All rights reserved.</string>
;
; ***************************************************************************************
; Configure Tool 1
;
; Commandline: 
;   MyAppData
; 
; Arguments:
;   "%FILE"
; 
; Working Directorory
;   Nothing
;
; Name:
;   MyAppData Run
;
; Event to trigger the tool:
;   After Compile/Run
; 
; Option:
;   Wait until tools quits
;
; ---------------------------------------------------------------------------------------
;
; Configure Tool 2
;
; Commandline: 
;   MyAppData
; 
; Arguments:
;   "%FILE"
; 
; Working Directorory
;   Nothing
;
; Name:
;   MyAppData Executable
;
; Event to trigger the tool:
;   After Create Executable
; 
; Option:
;   Nothing
;
; ---------------------------------------------------------------------------------------
;
; Format for new info.plist
; 
; Begin without space '; PLIST {keys and values}
;
; Example
; ; PLIST <key>CFBundleSignature</key>
; ; PLIST <string>PBAPP</string>
; ; PLIST <key>CFBundleShortVersionString</key>
; ; PLIST <string>1.0.1.0</string>
; ; PLIST <key>NSHumanReadableCopyright</key>
; ; PLIST <string>Copyright © 2018 mk-soft. All rights reserved.</string>

; **********************************************

EnableExplicit

Structure udtDict
  key.s
  List value.s()
EndStructure

Global NewList plist.s()
Global NewList dict.udtDict()

Global file_code.s
Global file_app.s
Global path_contents.s

; ---------------------------------------------------------------------------------------

Procedure InitParameters()
  ; Parameter source file
  file_code = ProgramParameter()
  If file_code = ""
    ProcedureReturn 0
  EndIf
  ; Parameter execute file
  file_app = GetEnvironmentVariable("PB_TOOL_Executable")
  If file_app = ""
    ProcedureReturn 0
  EndIf
  If GetExtensionPart(file_app) <> "app"
    ProcedureReturn 0
  EndIf
  ; Set path to contents
  path_contents = file_app + "/Contents/"
  ProcedureReturn 1
EndProcedure

; ---------------------------------------------------------------------------------------

;- Copy folder MyAppData to Contents
Procedure CopyAppData()
  Protected path_data.s
  
  path_data = GetPathPart(file_code) + "MyAppData"
  If FileSize(path_data) <> -2
    ProcedureReturn 0
  EndIf
  If CopyDirectory(path_data, path_contents, "*", #PB_FileSystem_Recursive) = 0
    MessageRequester("MyAppData", "Error copy data" + #CR$ + path_data + " to " + path_contents)
    ProcedureReturn 0
  EndIf
  ProcedureReturn 1
EndProcedure

; ---------------------------------------------------------------------------------------

;- Read info.plist from Application
Procedure ReadPList()
  Protected file_plist.s, text.s, dict.i, key.s, value.s, newkey.i, pos
  
  file_plist.s = path_contents + "info.plist"
  If ReadFile(0, file_plist)
    While Not Eof(0)
      text.s = ReadString(0, #PB_UTF8)
      If Bool(text)
        If FindString(text, "<dict>")
          AddElement(plist())
          plist() = text
          dict = #True
        ElseIf FindString(text, "</dict>")
          AddElement(plist())
          plist() = text
          dict = #False
        ElseIf Not dict
          AddElement(plist())
          plist() = text
        EndIf
        If dict
          pos = FindString(text, "<key>") 
          If pos >= 1 And pos <= 3
            AddElement(dict())
            dict()\key = Trim(text)
            newkey = #True
          ElseIf newkey
            AddElement(dict()\value())
            dict()\value() = Trim(text)
          EndIf
        EndIf
      EndIf
    Wend
    CloseFile(0)
  EndIf
EndProcedure

;- Read new info.plist from source
Procedure ReadInfo()
  Protected r1, text.s, key.s, lkey.s, value.s, newkey, addkey
  
  If ReadFile(0, file_code)
    While Not Eof(0)
      text.s = ReadString(0, #PB_UTF8)
      If Bool(text)
        If FindString(text, "; PLIST ", 1, #PB_String_NoCase) = 1
          r1 = #True
          text = RTrim(Mid(text, 9))
          If FindString(text, "<key>") = 1 ; New key only with first place
            key = Trim(text)
            lkey = LCase(key)
            newkey = #True
            addkey = #True
            ForEach dict()
              If LCase(dict()\key) = lkey
                ClearList(dict()\value())
                addkey = #False
                Break
              EndIf
            Next
            If addkey
              ResetList(dict())
              AddElement(dict())
              dict()\key = key
            EndIf
          ElseIf newkey
            AddElement(dict()\value())
            dict()\value() = text
          EndIf
        EndIf
      EndIf
    Wend
    CloseFile(0)
  EndIf
  ProcedureReturn r1
EndProcedure

;- Write new info.plist
Procedure WritePList()
  Protected file_plist.s
  
  file_plist.s = path_contents + "info.plist"
  If CreateFile(0, file_plist)
    ForEach plist()
      WriteStringN(0, plist(), #PB_UTF8)
      If FindString(plist(), "<dict>") = 1
        ForEach dict()
          WriteStringN(0, #TAB$ + dict()\key, #PB_UTF8)
          ForEach dict()\value()
            WriteStringN(0, #TAB$ + dict()\value(), #PB_UTF8)
          Next
        Next
      EndIf
    Next
  EndIf
EndProcedure

; ---------------------------------------------------------------------------------------

;-Main
If InitParameters()
  CopyAppData()
  ReadPList()
  If ReadInfo()
    WritePList()
  EndIf
EndIf
Last edited by mk-soft on Sun Jan 16, 2022 1:02 pm, edited 8 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PB IDE Tool MyAppData

Post by mk-soft »

Update v1.2.0.2
- Added: Replace and insert keys and values into 'info.plist'

Example

Code: Select all

;-TOP

; PLIST <key>CFBundleInfoDictionaryVersion</key>
; PLIST <string>6.0</string>
; PLIST <key>CFBundlePackageType</key>
; PLIST <string>APPL</string>
; PLIST <key>CFBundleVersion</key>
; PLIST <string>0.1</string>
; PLIST <key>CFBundleSignature</key>
; PLIST <string>PBAPP</string>
; PLIST <key>CFBundleShortVersionString</key>
; PLIST <string>1.0.2</string>
; PLIST <key>NSHumanReadableCopyright</key>
; PLIST <string>Copyright © 2018 mk-soft. All rights reserved.</string>

MessageRequester("Info", "Hello World!", #PB_MessageRequester_Info)
Last edited by mk-soft on Sun Dec 30, 2018 3:07 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PB IDE Tool MyAppData

Post by mk-soft »

Update v1.2.0.1
- Code cleanup

:wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PB IDE Tool MyAppData

Post by mk-soft »

Update v1.2.02
- Change syntax ";PLIST <...>" to ";[Space]PLIST <...>"
Last edited by mk-soft on Mon Feb 14, 2022 10:55 am, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PB IDE Tool MyAppData

Post by mk-soft »

Update PathHelper v1.05.1
- Change as module to use inside modules

Update PathHelper v1.05.2
- Bugfix compiler option

Code: Select all

;-TOP

; Comment : Module Path Helper v1.05.2 by mk-soft
; Link    : https://www.purebasic.fr/english/viewtopic.php?p=562634#p562634

DeclareModule PathHelper
  ; Change names
  CompilerIf Defined(CompanyName, #PB_Constant) = 0
  	#CompanyName = "mk-soft"
  CompilerEndIf
  
  CompilerIf Defined(ApplicationName, #PB_Constant) = 0
  	#ApplicationName = "MyApplication"
  CompilerEndIf
  
  Declare.s GetProgramPath()
  Declare.s GetResourcePath()
  Declare.s GetLibraryPath()
  Declare.s GetProgramDataPath()
  Declare.s GetAllUserDataPath()
  
EndDeclareModule
  
; ----

Module PathHelper
  
  CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
    
    Macro CocoaString(NSString)
      PeekS(CocoaMessage(0, NSString, "UTF8String"), -1, #PB_UTF8)
    EndMacro
    
  CompilerEndIf
  
  Procedure.s GetProgramPath()
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      Static bundlePath.s
      Protected bundlePathPtr
      
      If Not Bool(bundlePath)
        bundlePathPtr = CocoaMessage(0,CocoaMessage(0,0,"NSBundle mainBundle"),"bundlePath")
        If bundlePathPtr
          bundlePath = CocoaString(bundlePathPtr) + "/"
        EndIf
      EndIf
      ProcedureReturn bundlePath
    CompilerElse
      ProcedureReturn GetPathPart(ProgramFilename())  
    CompilerEndIf
  EndProcedure
  
  ; ----
  
  Procedure.s GetResourcePath()
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      Static resourcePath.s
      Protected resourcePathPtr
      
      If Not Bool(resourcePath)
        resourcePathPtr = CocoaMessage(0,CocoaMessage(0,0,"NSBundle mainBundle"),"resourcePath")
        If resourcePathPtr
          resourcePath = CocoaString(resourcePathPtr) + "/"
        EndIf
      EndIf
      ProcedureReturn resourcePath
    CompilerElse
      ProcedureReturn GetProgramPath() + "Resources" + #PS$
    CompilerEndIf
  EndProcedure
  
  ; ----
  
  Procedure.s GetLibraryPath()
    Protected librayPath.s
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      librayPath = GetProgramPath() + "Contents/Library/"
    CompilerElse
      librayPath = GetProgramPath() + "Library" + #PS$
    CompilerEndIf
    ProcedureReturn librayPath
  EndProcedure
  
  ; ----
  
  Procedure.s GetProgramDataPath()
    Protected basePath.s, subPath.s, dataPath.s
    
    CompilerIf #PB_Compiler_OS = #PB_OS_Linux
      basePath = GetHomeDirectory()
      subPath = basePath + "." + #CompanyName + #PS$
    CompilerElse
      basePath = GetUserDirectory(#PB_Directory_ProgramData)
      subPath = basePath + #CompanyName + #PS$
    CompilerEndIf
    dataPath = subPath + #ApplicationName + #PS$
    If FileSize(dataPath) <> -2
      If FileSize(subPath) <> -2
        CreateDirectory(subPath)
      EndIf
      If FileSize(dataPath) <> -2
        CreateDirectory(dataPath)
      EndIf
    EndIf
    ProcedureReturn dataPath
  EndProcedure
  
  ; ----
  
  Procedure.s GetAllUserDataPath()
    Protected basePath.s, subPath.s, dataPath.s
    
    basePath = GetUserDirectory(#PB_Directory_AllUserData)
    subPath = basePath + #CompanyName + #PS$
    dataPath = subPath + #ApplicationName + #PS$
    If FileSize(dataPath) <> -2
      If FileSize(subPath) <> -2
        CreateDirectory(subPath)
      EndIf
      If FileSize(dataPath) <> -2
        CreateDirectory(dataPath)
      EndIf
    EndIf
    ProcedureReturn dataPath
  EndProcedure
  
  ; ----
  
EndModule

UseModule PathHelper

; ****

CompilerIf #PB_Compiler_IsMainFile
  Debug "Program Path: " + GetProgramPath()
  Debug "Program Resources Path: " + GetResourcePath()
  Debug "Program Libraries Path: " + GetLibraryPath()
  Debug "Program Data Path: " + GetProgramDataPath()
  Debug "Program Alluser Data Path: " + GetAllUserDataPath()
CompilerEndIf
Last edited by mk-soft on Sun Jul 23, 2023 9:14 am, edited 6 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
mrbungle
Enthusiast
Enthusiast
Posts: 111
Joined: Wed Dec 30, 2020 3:18 am

Re: PB IDE Tool MyAppData

Post by mrbungle »

Dumb question(s). Do we compile these files first? Where does tool 2 start? Thank you.
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PB IDE Tool MyAppData

Post by mk-soft »

First compile the tool as "MyAppData" and then add it to PB's tools (2 times).
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
mrbungle
Enthusiast
Enthusiast
Posts: 111
Joined: Wed Dec 30, 2020 3:18 am

Re: PB IDE Tool MyAppData

Post by mrbungle »

Got it. Thank you!
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PB IDE Tool MyAppData

Post by mk-soft »

Update MyAppData v1.03.1
- Build with compilermode console

Update PathHelper v1.03.0
- Added functions
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
HeX0R
Addict
Addict
Posts: 973
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: PB IDE Tool MyAppData

Post by HeX0R »

I like your PathHelper, but to make it more flexible and usable as a common include file, you maybe should change the first two lines into:

Code: Select all

CompilerIf Defined(CompanyName, #PB_Constant) = 0
	#CompanyName = "mk-soft"
CompilerEndIf
CompilerIf Defined(ApplicationName, #PB_Constant) = 0
	#ApplicationName = "MyApplication"
CompilerEndIf
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PB IDE Tool MyAppData

Post by mk-soft »

@Hexor

Ok, done :wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PB IDE Tool MyAppData

Post by mk-soft »

Update v1.04.0
- Added check is Application
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PB IDE Tool MyAppData

Post by mk-soft »

Update PathHelper v1.05.1
- Change as module to use inside modules
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
HeX0R
Addict
Addict
Posts: 973
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: PB IDE Tool MyAppData

Post by HeX0R »

PathHelper v1.05.1

Code: Select all

CompilerIf #PB_Compiler_OS   ;<-- ???
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PB IDE Tool MyAppData

Post by mk-soft »

Thanks HeXOR;)

Update PathHelper v1.05.2
- Bugfix compiler option

Looks decent now, although it didn't lead to any errors as the compiler automatically removes them if not needed.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply