Terminal.app: Import Custom Profile & Apply it to current tab's script


importNewCustomProfile("Custom Profile 2.terminal") -- here, assumed it is stored on the desktop 

tell application "Terminal"
	set customProfile to 1st settings set whose name is "Custom Profile 2"
	set theTab to do script "" -- make new tab, remember it
	tell theTab
		set its current settings to customProfile
		do script "echo 'Hello with new Custom Profile 2'" in it
	end tell
end tell

on importNewCustomProfile(profileName)
	set terminalProfileFileHFS to ((path to desktop) as text) & profileName
	tell application "Terminal" to open terminalProfileFileHFS
end importNewCustomProfile

Other (hack) approach. It allows Export & Import Terminal Profiles not as canonical Terminal Settings files , but as usual text files with custom record in it:

(NOTE: you can’t export programatically profiles using Terminal scripting dictionary, without GUI scripting). But you can’t use created “hacked” way text custom profile file as canonical Terminal Profile file. Don’t try.

I would call my method “Create the settings file yourself, then apply it to the tab of the window you want”.

Step 1: Creating the Terminal Custom Settings text (!!!) file:


-- script: Create Terminal Profile text file (yourself)

set desktopHFS to (path to desktop folder) as text
set fileRef to open for access file (desktopHFS & "h.terminal") with write permission
set eof of fileRef to 0
write {|custom title|:"Testing the tab", |font size|:20, |background color|:{5866, 5865, 5866}, |bold text color|:{65535, 65533, 65534}, |cursor color|:{35700, 0, 0}, |font antialiasing|:true, |font name|:"Menlo-Regular", |normal text color|:{65535, 65533, 65534}, |name|:"Custom Profile 1", |number of columns|:80, |number of rows|:24, |title displays custom title|:true, |title displays device name|:false, |title displays settings name|:true, |title displays shell path|:false, |title displays window size|:true, |clean commands|:{"screen", "tmux"}} to fileRef
close access fileRef

Step 2: Executing script in the Terminal using Custom Profile text File


-- script: execute script in the Terminal using Custom Profile text File

set desktopHFS to (path to desktop folder) as text
set customProfile to read (file (desktopHFS & "h.terminal")) as record

tell application "Terminal"
    -- assuming you have opened tab 1 of window 1
    -- you can indicate in the code line below your tab of your window
    tell tab 1 of window 1 to tell current settings
        set background color to |background color| of customProfile
        set bold text color to |bold text color| of customProfile
        set clean commands to |clean commands| of customProfile
        set cursor color to |cursor color| of customProfile --> (red)
        set custom title to |custom title| of customProfile
        set font antialiasing to |font antialiasing| of customProfile
        set font name to |font name| of customProfile
        set font size to |font size| of customProfile
        set name to |name| of customProfile
        set normal text color to |normal text color| of customProfile
        set number of columns to |number of columns| of customProfile
        set number of rows to |number of rows| of customProfile
        set title displays custom title to |title displays custom title| of customProfile
        set title displays device name to |title displays device name| of customProfile
        set title displays settings name to |title displays settings name| of customProfile
        set title displays shell path to |title displays shell path| of customProfile
        set title displays window size to |title displays window size| of customProfile
    end tell
    -- assuming you have opened tab 1 of window 1
    -- you can indicate in the code line below your tab of your window
    do script "echo 'Hello,word!'" in tab 1 of window 1 -- executes script in the same tab
end tell