Today's focus Bike template

How can this Apple script, Bike document template, be modified to:

  • Append the current date to the file name?
  • Create the hierarchy shown in the attached screen capture?
    Thank you. Stan

For the date part, you could derive a yyyy-mm-dd date string like this:

on run
    text 1 thru 10 of taskPaperDateString(current date)
end run


-- taskPaperDateString :: Date -> String
on taskPaperDateString(dte)
    set {d, t} to splitOn("T", dte as «class isot» as string)
    d & space & text 1 thru 5 of t
end taskPaperDateString


-- splitOn :: String -> String -> [String]
on splitOn(pat, src)
    set {dlm, my text item delimiters} to ¬
        {my text item delimiters, pat}
    set xs to text items of src
    set my text item delimiters to dlm
    return xs
end splitOn

And for your template, perhaps this kind of pattern:

on run
    tell application "Bike"
        
        set doc to front document
        
        if exists doc then
            tell doc
                repeat with n in {1, 2, 3}
                    tell (make row with properties {name:"Project " & (n as string)})
                        make row with properties {name:"Research ideas"}
                    end tell
                end repeat
            end tell
        else
            "No document open in Bike"
        end if
        
    end tell
end run

PS you probably now that you can suppress the default welcome text with a Preferences setting.

Bike > Preferences > Document > New: (unchecking Show welcome text)

Or, if you want to define your template in terms of XML (which would allow, for example, for some formatted text):

on run
    tell application "Bike"
        
        set doc to front document
        
        if exists doc then
            tell doc
                import from my xmlSample() as bike format
            end tell
        else
            "No document open in Bike"
        end if
        
    end tell
end run


on xmlSample()
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<html><head><meta charset=\"utf-8\"/></head><body><ul><li><p><strong>Project 1</strong></p><ul><li><p><em>Research ideas</em></p></li><li><p/></li></ul></li><li><p><strong>Project 2</strong></p><ul><li><p><em>Research ideas</em></p></li><li><p/></li></ul></li><li><p><strong>Project 3</strong></p><ul><li><p><em>Research ideas</em></p></li><li><p/></li></ul></li><li><p/></li></ul></body>\n</html>"
end xmlSample

Though that kind of approach may be simpler to manage in JavaScript:

Expand disclosure triangle to view JS source
(() => {
    "use strict";

    const main = () => {
        const
            bike = Application("Bike"),
            doc = bike.documents.at(0);

        return doc.exists() ? (
            doc.import({
                from: xmlTemplate(),
                as: "bike format"
            })
        ) : "No documents open in Bike";
    };

    const xmlTemplate = () => `<?xml version="1.0" encoding="UTF-8"?>
<html>
  <head>
    <meta charset="utf-8"/>
  </head>
  <body>
    <ul>
      <li>
        <p><strong>Project 1</strong></p>
        <ul>
          <li>
            <p><em>Research ideas</em></p>
          </li>
          <li>
            <p/>
          </li>
        </ul>
      </li>
      <li>
        <p><strong>Project 2</strong></p>
        <ul>
          <li>
            <p><em>Research ideas</em></p>
          </li>
          <li>
            <p/>
          </li>
        </ul>
      </li>
      <li>
        <p><strong>Project 3</strong></p>
        <ul>
          <li>
            <p><em>Research ideas</em></p>
          </li>
          <li>
            <p/>
          </li>
        </ul>
      </li>
      <li>
        <p/>
      </li>
    </ul>
  </body>
</html>`;

    return main();
})();

@complexpoint @jessegrosjean
Thank you everyone for your help.
The pieces are coming together.
I’ll add this date solution form alastor933.


1 Like

Another approach might be to automatically import the template pattern you want from an external file.

Not tested, but roughly this kind of thing:

Expand disclosure triangle to view AppleScript source
use framework "Foundation"
use scripting additions

-- EDIT THESE TO SPECIFY NOTES FOLDER AND TEMPLATE TO IMPORT

property notesFolderPath : "~/Desktop"
property templateFileName : "researchTemplate.bike"


--------------------------- MAIN -------------------------
on run
	dayNoteFoundOrCreated("ResearchNotes")
end run


-- dayNoteFoundOrCreated :: String -> IO document
on dayNoteFoundOrCreated(prefix)
	tell (current date) as «class isot»
		set dateString to text 1 thru 10 of (it as string)
	end tell
	
	set noteName to (prefix & dateString & ".bike")
	
	set fpNotes to filePath(combine(notesFolderPath, noteName))
	set fpTemplate to filePath(combine(notesFolderPath, templateFileName))
	
	do shell script unlines({"touch " & fpNotes, "open " & fpNotes})
	delay 0.2
	tell application "Bike"
		set doc to front document
		
		set n to (count of (rows of doc where name is not ""))
		if 0 = n then
			if my doesFileExist(fpTemplate) then
				set maybeXML to my readFile(fpTemplate)
				
				if maybeXML starts with "<?xml" then
					tell doc
						import from maybeXML as bike format
						
						"[" & its name & "](" & its URL & ")"
					end tell
				else
					"No bike format document found in:" & ¬
						linefeed & tab & fpTemplate
				end if
			else
				"Template file not found at" & ¬
					linefeed & tab & fpTemplate
			end if
		end if
	end tell
end dayNoteFoundOrCreated


------------------------- GENERIC ------------------------


-- combine (</>) :: FilePath -> FilePath -> FilePath
on combine(fp, fp1)
	-- The concatenation of two filePath segments,
	-- without omission or duplication of "/".
	if "" = fp or "" = fp1 then
		fp & fp1
	else if "/" = item 1 of fp1 then
		fp1
	else if "/" = item -1 of fp then
		fp & fp1
	else
		fp & "/" & fp1
	end if
end combine


-- doesFileExist :: FilePath -> IO Bool
on doesFileExist(strPath)
	set ca to current application
	set oPath to (ca's NSString's stringWithString:strPath)'s ¬
		stringByStandardizingPath
	set {bln, int} to (ca's NSFileManager's defaultManager's ¬
		fileExistsAtPath:oPath isDirectory:(reference))
	bln and (1 ≠ int)
end doesFileExist



-- filePath :: String -> FilePath
on filePath(s)
	((current application's ¬
		NSString's stringWithString:s)'s ¬
		stringByStandardizingPath()) as string
end filePath


-- readFile :: FilePath -> IO String
on readFile(strPath)
	set ca to current application
	set e to reference
	set {s, e} to (ca's NSString's ¬
		stringWithContentsOfFile:((ca's NSString's ¬
			stringWithString:strPath)'s ¬
			stringByStandardizingPath) ¬
			encoding:(ca's NSUTF8StringEncoding) |error|:(e))
	if missing value is e then
		s as string
	else
		(localizedDescription of e) as string
	end if
end readFile


-- unlines :: [String] -> String
on unlines(xs)
	-- A single string formed by the intercalation
	-- of a list of strings with the newline character.
	set {dlm, my text item delimiters} to ¬
		{my text item delimiters, linefeed}
	set s to xs as text
	set my text item delimiters to dlm
	s
end unlines

Assuming a .bike file like the unzipped version of this as the template:

researchTemplate.bike.zip (1.2 KB)