ホームページ開発ツール>Xojo / Real Studio Trial and Error・CocoaのDeclareでツールバーを実装する・アイコンをカスタマイズする - 別稿

 svgファイルからAssets.carファイルを生成するツールのサンプルです。

 ListBoxに、svgファイル一式をドラッグ&ドロップ後、ボタンを押すとデスクトップに、Assets.carファイルが生成されます。
 一時的に、デスクトップにAssets.xcassetsフォルダーを生成しますが、処理後は不要なので、ゴミ箱に捨てて下さい。(または削除機能を追加して下さい。)

S Shot 1
  1. Xojoで新規プロジェクトを作成
  2. Window1に、Listbox(Name:Listbox1、ColumnCount : 2、ColumnWidths : 100%,0)、PushButton(Name:PushButton1)を追加
  3. 以下をListbox1にペースト(できなければ、Sub - Endの間をDropObjectイベントに記述)
    Sub DropObject(obj As DragItem, action As Integer) Handles DropObject
      if obj.FolderItemAvailable then
        
        do
          
          // ドロップしたファイルの名前とパスをセット
          ListBox1.AddRow ""
          ListBox1.Cell(ListBox1.LastIndex,0) = obj.FolderItem.Name
          ListBox1.Cell(ListBox1.LastIndex,1) = obj.FolderItem.NativePath
          
        loop until not obj.NextItem  // 複数ドロップ対応
        
        // 実行ボタンを有効に
        PushButton1.Enabled=true
        
      end if
    End Sub
    
  4. 以下をListbox1にペースト(できなければ、Sub - Endの間をOpenイベントに記述)
    Sub Open() Handles Open
      me.AcceptFileDrop("special/folder")
    End Sub
    
  5. 以下をPushButton1にペースト(できなければ、Sub - Endの間をActionイベントに記述)
    Sub Action() Handles Action
      // Assetsフォルダー作成(既に存在したら中止)
      if MakeFolder() then
        
        // actoolの実行
        DoShellActool()
        
      end if
    End Sub
    
  6. 以下をWindow1にペースト(できなければ、Sub - Endの間をOpenイベントに記述)
    Sub Open() Handles Open
      // ルートパスの取得(ここではデスクトップ)
      Dim f As FolderItem = SpecialFolder.Desktop
      RootPath=f.NativePath
      
      // 実行ボタンを無効に
      PushButton1.Enabled=false
    End Sub
    
  7. 以下をWindow1にペースト
    Private Sub DoShellActool()
      // シェルコマンドの実行(デスクトップに移動して、actoolでAssets.car生成。生成場所はカレントディレクトリー)
      Dim s As Shell = new Shell
      s.Execute("cd "+RootPath+" ; xcrun actool Assets.xcassets --compile . --platform macosx --minimum-deployment-target 10.10" )
      
      if s.ErrorCode = 0 then
      else
        MsgBox("Error code: " + Str(s.ErrorCode))
      end if
    End Sub
    
  8. 以下をWindow1にペースト
    Private Function ExtractBody(st As String) as String
      Dim i, cnt As Integer
      Dim body As String
      
      // ピリオドの数をカウント
      cnt=CountFields(st,".")
      
      // 最後のピリオド以前をbodyとみなす
      body=""
      for i=1 to CountFields(st,".")-2
        body=body+NthField(st,".",i)+"."
      next
      body=body+NthField(st,".",cnt-1)
      
      return body
    End Function
    
  9. 以下をWindow1にペースト
    Private Function MakeFolder() as Boolean
      // デスクトップにAssets.xcassetsフォルダーを取得
      Dim f As FolderItem = GetFolderItem(RootPath+"/Assets.xcassets",3)
      
      // Assets.xcassetsは一時フォルダーであり、既に存在するのは想定外なので処理を中止
      if f<>nil and f.Exists then
        MsgBox "既にAssetsフォルダーが作成済です。取り除かないと続行できません。"
        return false
      end if
      
      f.CreateAsFolder  // フォルダー生成
      
      MakeJsonAssets(f)  // jsonファイル生成
      
      Dim body As String
      Dim f2, f3 As FolderItem
      for i As Integer = 0 to Listbox1.ListCount-1
        
        // 元のsvgファイル取得
        f2=GetFolderItem(Listbox1.Cell(i,1),3)
        
        // Assets.xcassetsフォルダー内に、svgファイル名の拡張子をsymbolsetに変更したフォルダーを取得
        body=ExtractBody(f2.Name)  // ファイル名から拡張子を取り除く
        f3=GetFolderItem(f.Child(body+".symbolset").NativePath,3)
        if f3=nil or f3.Exists=false then
          
          f3.CreateAsFolder  // フォルダー生成
          
          f2.CopyFileTo f3  // 元のsvgファイルを上記フォルダー内にコピー
          
          MakeJsonSvg(f3,f2.Name)  // jsonファイル生成
          
        end if
        
      next
      
      return true
    End Function
    
  10. 以下をWindow1にペースト
    Private Sub MakeJsonAssets(f As FolderItem)
      Dim f2 As FolderItem = f.Child("Contents.json")
      if f2 = nil then
        return
      end if
      
      Dim t As TextOutputStream = TextOutputStream.Create(f2)
      
      t.WriteLine("{")
      t.WriteLine("  ""info"" : {")
      t.WriteLine("    ""author"" : ""xcode"",")
      t.WriteLine("    ""version"" : 1")
      t.WriteLine("  }")
      t.WriteLine("}")
      
      t.Close
    End Sub
    
  11. 以下をWindow1にペースト
    Private Sub MakeJsonSvg(f As FolderItem, fname As String)
      Dim f2 As FolderItem = f.Child("Contents.json")
      if f2 = nil then
        return
      end if
      
      Dim t As TextOutputStream = TextOutputStream.Create(f2)
      
      t.WriteLine("{")
      t.WriteLine("  ""info"" : {")
      t.WriteLine("    ""author"" : ""xcode"",")  // xcodeではないけど、問題はなさそう
      t.WriteLine("    ""version"" : 1")
      t.WriteLine("  },")
      t.WriteLine("  ""symbols"" : [")
      t.WriteLine("    {")
      t.WriteLine("      ""filename"" : """+fname+""",")
      t.WriteLine("      ""idiom"" : ""universal""")
      t.WriteLine("    }")
      t.WriteLine("  ]")
      t.WriteLine("}")
      
      t.Close
    End Sub
    
  12. 以下をWindow1にペースト(できなければプロパティに、名前:RootPath、データ型:String、を追加)
    Private Property RootPath as String
    

[Home]  [MacSoft]  [Donation]  [History]  [Privacy Policy]  [Affiliate Policy]