This script creates or updates a User Attribute with Tinderbox’s current folder URL.
It makes it easier to update relative links to resources after a Tinderbox or its folder has been renamed or moved.
I store all Tinderbox documents in their own local git repo and use relative links to a resources folder inside the repo. However, I also tend to rename repos quite often and always move them after I’m done. This of course breaks the links. The script makes it super easy to update them.
How it works
-
If it doesn’t exist a User Attribute is created and its default value is set to Tinderbox’s current folder URL.
-
If it exists then its default value is set to Tinderbox’s current folder URL, i.e. it’s updated.
Setup
- Set property
theAttributeName
to a name of your liking
Usage
- Open a Tinderbox
- Run script
Result
The script results in a User Attribute whose default value is e.g.
file:///Users/user/Documents/Tinderbox/Bilder%20relativ%20zur%20Tinderbox%20verlinken/
It can then be used in a notes $Text
, e.g. as an relative img
link:
<img src="^value($URLSelfDirectory)^^value(urlEncode($MediaDirectory))^^value(urlEncode($MediaName))^">
References
Please see @satikusala’s videos on “Working with media”
- Tinderbox Training Video 12 - Working with media in your TBX files (Part 1)
- Tinderbox Training Video 13 - Working with media in your TBX files (Part 2)
@satikusala your videos and Tinderbox documents are very helpul, thanks a lot!
-- Create or update attribute with Tinderbox's current folder URL for relative linking
-- Makes usage of relative links and moving around a Tinderbox easier
-- Result: a User Attribute's default value set to the Tinderbox's current folder URL, e.g. "file:///Users/user/Documents/Tinderbox/Bilder%20relativ%20zur%20Tinderbox%20verlinken/"
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
property theAttributeName : "URLSelfDirectory"
tell application id "com.eastgate.Tinderbox-8"
try
set theDocument to front document
set theDocumentPath to POSIX path of (file of theDocument as string)
set theDirectoryURL to my directoryURL(theDocumentPath)
if not (exists attribute theAttributeName in theDocument) then
set theAttribute to make attribute in theDocument
set type of theAttribute to "URL"
set defaultValue of theAttribute to theDirectoryURL
set name of theAttribute to theAttributeName
display notification theDirectoryURL with title "Created Attribute \"" & theAttributeName & "\""
else
set defaultValue of attribute theAttributeName in theDocument to theDirectoryURL
display notification theDirectoryURL with title "Updated Attribute \"" & theAttributeName & "\""
end if
on error error_message number error_number
if the error_number is not -128 then display alert "Tinderbox" message error_message as warning
return
end try
end tell
on directoryURL(thePath)
try
set thePath to (current application's NSString's stringWithString:thePath)
set theDirectory to thePath's stringByDeletingLastPathComponent()
set theDirectory_URL to current application's |NSURL|'s fileURLWithPath:theDirectory
return (theDirectory_URL's absoluteString()) as string
on error error_message number error_number
activate
if the error_number is not -128 then display alert "Error: Handler \"directoryURL\"" message error_message as warning
error number -128
end try
end directoryURL