Fandom

Foldit Wiki

VBScript to insert recipe in 'all.macro'

1,448pages on
this wiki
Add New Page
Comments0 Share

Note: this method is obsolete. Current versions of all.macro have a digital signature, in the form of the "verify" keyword at the end of the file. The signature likely means changes by outside tools like this VB script won't be successful. Also the Foldit recipe editor now does import/export of recipes, moving them between the cookbook and the file system. Nevertheless, this script served a purpose at a point in time. We'll keep it around for reference.

See Editing Foldit Recipes for the user-friendly way to work with an external editor.

This quick and dirty VBScript inserts a scriptfile into 'all.macro'.

Option Explicit

' ##############################################
' # VBScript to insert recipe in all.macro
' # Takes an ASCII-Textfile via OpenDialog with
' # your script and insert it in 'all.macro'
' # Tested on WinXP {modify it}
' # use at your own risk and backup 'all.macro',
' # before use this script (if you insert the wrong file)
' # Set the variable 'MacroFilename' to the
' # file and path to 'all.macro' and place the
' # VBScript everywhere
' # Don't use special characters in Recipe-name
' # and Recipe-description
' ##############################################

Dim Description, RecipeName, Stream, Scripttext, Macrotext
Dim Temp, ScriptFilename, MacroFilename, FSO, File
Dim k, enc, IsASCII, OpenDialog, InitOpenDialog
' Path to all.macro | edit to your own settings
MacroFilename = "C:\Dokumente und Einstellungen\All Users\Anwendungsdaten\foldit\all.macro"

Sub Replace_it ( ByRef s )
   s = Replace(s, "\n", "\\\\n") ' only '\n' in the script will be converted
   s = Replace(s, Chr(9), Space(4)) ' convert tab-char to 4 spaces
   s = Replace(s, vbcrlf, "\\n") ' convert crlf
   s = Replace(s, vblf, "\\n") ' convert line feed, if used in the script-file (linux)
   s = Replace(s, ",", "\\\,") ' convert ','
   s = Replace(s, """", "\\\""") ' convert '"'
End Sub

' The OpenDialog for the file to insert
Set OpenDialog = CreateObject("UserAccounts.CommonDialog")
OpenDialog.Filter = "All Files|*.*"
OpenDialog.FilterIndex = 3
InitOpenDialog = OpenDialog.ShowOpen
If InitOpenDialog = False Then
  Wscript.Echo "Script Error: Please select a file!"
  Wscript.Quit
Else
  ScriptFilename = OpenDialog.FileName
End If

Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(ScriptFilename) And FSO.FileExists(MacroFilename) Then
  Set File = FSO.GetFile(ScriptFilename)
  Set Stream = File.OpenAsTextStream(1,0)
  Scripttext = Stream.ReadAll
  Stream.Close
  Set File = FSO.GetFile(MacroFilename)
  Set Stream = File.OpenAsTextStream(1,0)
  Macrotext = Stream.ReadAll
  Stream.Close
  IsASCII = Encoding(Scripttext)
  If enc = vbfalse Then
    Msgbox "Error! " & IsASCII & " File must be ASCII."
    WScript.Quit
  End If

  Temp = " ""$$recipenumber$$"" : ""{"
  Temp = Temp & "\n \""desc\"" : \""$$description$$\"""
  Temp = Temp & "\n \""mid\"" : \""0\"""
  Temp = Temp & "\n \""mrid\"" : \""0\"""
  Temp = Temp & "\n \""name\"" : \""$$recipename$$\"""
  Temp = Temp & "\n \""parent\"" : \""0\"""
  Temp = Temp & "\n \""parent_mrid\"" : \""0\"""
  Temp = Temp & "\n \""player_id\"" : \""0\"""
  Temp = Temp & "\n \""script\"" : \""$$recipe$$\"""
  Temp = Temp & "\n \""type\"" : \""script\"""
  Temp = Temp & "\n \""uses\"" : \""0\"""
  Temp = Temp & "\n \""ver\"" : \""0.3\"" \n}\n"""
  Recipename = InputBox("Enter a name for the recipe", "Input")
  Description = InputBox("Enter a description for the recipe", "Input")
  If Recipename="" Then Recipename = "unnamed"
  If Description="" Then Description = "no description"
    k = InStrRev(Macrotext, "}")
    If k>0 Then
      Replace_it Scripttext
      Temp = Replace(Temp, "$$recipe$$", Scripttext)
      Temp = Replace(Temp, "$$recipename$$", Recipename)
      Temp = Replace(Temp, "$$description$$", Description)
      Temp = Left(Macrotext, k - 1) & Temp & vbcrlf & Right(Macrotext, Len(Macrotext) - k + 1)
      Temp = Replace(Temp, "$$recipenumber$$", Now)
      Set File = FSO.GetFile(MacroFilename)
      Set Stream = File.OpenAsTextStream(2,0)
      Stream.Write(Temp)
      Stream.Close
      Msgbox "File " & ScriptFilename & " inserted in " & vbcrlf & MacroFilename
    Else
      Msgbox "Oops! Can't find last '}'"
    End If
Else
   Msgbox "Scriptfile or all.macro does not exist."
End If

' Check Byte-order-mark
Function Encoding ( s )
  If Mid(s,1,1)=Chr(&HEF) Or Mid(s,2,1)=Chr(&HBB) Or Mid(s,1,1)=Chr(&HBF) Then
    ' UTF-8
    enc = vbfalse
    Encoding = "Format is UTF-8."
  ElseIf ( Mid(s,1,1)=Chr(&HFE) And Mid(s,2,1)=Chr(&HFF) ) Then
    ' UTF-16BE  {Big Endian}
    enc = vbfalse
    Encoding = "Format is UTF-16 Big Endian."
  ElseIf ( Mid(s,1,1)=Chr(&HFF) And Mid(s,2,1)=Chr(&HFE) ) Then
    ' UTF-16LE  {Little Endian}
    enc = vbfalse
    Encoding = "Format is UTF-16 Little Endian."
  ElseIf ( Mid(s,1,1)=Chr(&H00) And Mid(s,2,1)=Chr(&H00) _
       And Mid(s,3,1)=Chr(&HFE) And Mid(s,4,1)=Chr(&HFF) ) Then
    ' UTF-32BE  {Big Endian}
    enc = vbfalse
    Encoding = "Format is UTF-32 Big Endian."
  ElseIf ( Mid(s,1,1)=Chr(&HFF) And Mid(s,2,1)=Chr(&HFE) _
       And Mid(s,3,1)=Chr(&H00) And Mid(s,4,1)=Chr(&H00) ) Then
    ' UTF-32LE  {Little Endian}
    enc = vbfalse
    Encoding = "Format is UTF-32 Little Endian."
  Else
    ' ASCII
    enc = vbtrue
    Encoding = "Format is ASCII"
  End If
End Function

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.