Pasteboard
A pasteboard (also referred to as “the clipboard”) temporarily holds representations of items of different types for transfer between different applications or different locations in the application.
Class Properties
general (Pasteboard r/o) • The Pasteboard used for user-initiated copy/paste support.
Class Functions
makeUnique() → (Pasteboard) • Creates a new unique pasteboard.
Instance Properties
URL (URL or null) • Gets or sets the pasteboard content as a single URL.
URLs (Array of URL or null) • Gets or sets the pasteboard content as a list of URLs.
color (Color or null) • Gets or sets the pasteboard content as a single color.
colors (Array of Color or null) • Gets or sets the pasteboard content as a list of colors.
hasColors (boolean r/o) • Returns true if the pasteboard contains one or more colors.
hasImages (boolean r/o) • Returns true if the pasteboard contains one or more images.
hasStrings (boolean r/o) • Returns true if the pasteboard contains one or more strings.
hasURLs (boolean r/o) • Returns true if the pasteboard contains one or more URLs.
image (Image or null) • Gets or sets the pasteboard content as a single image.
images (Array of Image or null) • Gets or sets the pasteboard content as a list of images.
items (Array of Pasteboard.Item) • The array of individual items on the pasteboard, each potentially with their own set of types.
string (String or null) • Gets or sets the pasteboard content as a single plain-text string.
strings (String or null) • Gets or sets the pasteboard content as a list of plain-text strings.
types (Array of TypeIdentifier r/o) • The list of pasteboard types currently available on the pasteboard.
Log User Clipboard Text
if (Pasteboard.general.hasStrings){
console.log(Pasteboard.general.string)
} else {console.log('No Clipboard Text')}
Set User Pasteboard String
Pasteboard.general.string = "How Now Brown Cow"
console.log(Pasteboard.general.string)
Set/Get Temporary Pasteboard Text
var tempClip = Pasteboard.makeUnique()
tempClip.string = "The rain in Spain falls mainly on the plain."
console.log(tempClip.string)
NOTE: altering the content of a unique pasteboard does not effect the contents of the user pasteboard (main clipboard).
Save Plain-Text from Pasteboard to File
if(Pasteboard.general.hasStrings){
var data = Data.fromString(Pasteboard.general.string)
var wrapper = FileWrapper.withContents('Pasteboard.txt', data)
var filesaver = new FileSaver()
var fileSaverPromise = filesaver.show(wrapper)
fileSaverPromise.then(function(urlObj){
console.log(urlObj.string)
urlObj.open()
})
fileSaverPromise.catch(function(err){
console.error(err.message)
})
}
Instance Functions
availableType(types: Array of TypeIdentifier) → (TypeIdentifier or null) • The first type from the provided list which is available on the pasteboard, or null if none are available.
addItems(items: Array of Pasteboard.Item) → ( ) • Appends the new items to the pasteboard.
clear() → ( ) • Remove all items from the pasteboard.
dataForType(type: TypeIdentifier) → (Data or null) • The Data representation for the given type in this pasteboard, or null if none is available.
setDataForType(data: Data, type: TypeIdentifier) → ( ) • Set the Data representation for the given type in this pasteboard, replacing any previously set data.
stringForType(type: TypeIdentifier) → (String or null) • The String representation for the given type in this pasteboard, or null if none is available.
setStringForType(string: String, type: TypeIdentifier) → ( ) • Set the String representation for the given type in this pasteboard, replacing any previously set data.
Save Pasteboard Image to File
if(Pasteboard.general.hasImages){
var data = Pasteboard.general.dataForType(TypeIdentifier.png)
var wrapper = FileWrapper.withContents('IMG-CLIP.png', data)
var filesaver = new FileSaver()
var fileSaverPromise = filesaver.show(wrapper)
fileSaverPromise.then(function(urlObj){
console.log(urlObj.string)
urlObj.open()
})
fileSaverPromise.catch(function(err){
console.error(err.message)
})
}
Get Specific Type from Pasteboard
var targetType = new TypeIdentifier("public.utf8-plain-text")
if(Pasteboard.general.availableType([targetType])){
var items = Pasteboard.general.items
var i
for (i = 0; i < items.length; i++) {
var data = items[i].dataForType(targetType)
if(data){
console.log(data.toString())
break
}
}
var wrapper = FileWrapper.withContents('Pasteboard.txt', data)
var filesaver = new FileSaver()
var fileSaverPromise = filesaver.show(wrapper)
fileSaverPromise.then(function(urlObj){
console.log(urlObj.string)
urlObj.open()
})
fileSaverPromise.catch(function(err){
console.error(err.message)
})
}
Pasteboard.Item Class
A pasteboard item is the generic instance of the Pasteboard.Item class, a single instance of one of the data types supported by the Pasteboard class.
new Pasteboard.Item() → (Pasteboard.Item) • Make a new empty pasteboard item with no contents.
Instance Properties
The properties of an instance of the Pasteboard.Item class:
types (Array of TypeIdentifier r/o) • The list of types available for this pasteboard item.
Instance Functions
The functions used to manipulate an instance of the Pasteboard.Item class:
dataForType(type:TypeIdentifier) → (Data or null) • The Data representation for the given type in this pasteboard item, or null if none is available.
setDataForType(data:Data, type:TypeIdentifier) → ( ) • Set the Data representation for the given type in this pasteboard item, replacing any previously set data.
stringForType(type:TypeIdentifier) → (String or null) • The String representation for the given type in this pasteboard item, or null if none is available.
setStringForType(string:String, type:TypeIdentifier) → ( ) • Set the String representation for the given type in this pasteboard item, replacing any previously set data.
Place Image from Data on Pasteboard
var img64 = "iVBORw0KGgoAAAANSUhEUgAAABMAAAASCAYAAAC5DOVpAAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAAgtpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IlhNUCBDb3JlIDUuNC4wIj4KICAgPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICAgICAgPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIKICAgICAgICAgICAgeG1sbnM6dGlmZj0iaHR0cDovL25zLmFkb2JlLmNvbS90aWZmLzEuMC8iPgogICAgICAgICA8dGlmZjpSZXNvbHV0aW9uVW5pdD4yPC90aWZmOlJlc29sdXRpb25Vbml0PgogICAgICAgICA8dGlmZjpDb21wcmVzc2lvbj41PC90aWZmOkNvbXByZXNzaW9uPgogICAgICAgICA8dGlmZjpPcmllbnRhdGlvbj4xPC90aWZmOk9yaWVudGF0aW9uPgogICAgICAgICA8dGlmZjpQaG90b21ldHJpY0ludGVycHJldGF0aW9uPjI8L3RpZmY6UGhvdG9tZXRyaWNJbnRlcnByZXRhdGlvbj4KICAgICAgPC9yZGY6RGVzY3JpcHRpb24+CiAgIDwvcmRmOlJERj4KPC94OnhtcG1ldGE+CobSriQAAAGeSURBVDgRjZS/K4VhFMfv9TOykM0iPzJgkDIYDCyUxSIpi0kZDAaDSUb8CQYmKWGQRXdQykgMSukOlIVIym8+3+s5b8frva5Tn3vOc873fd7ned7z3HTqb2uh3ABlcAVH8Ab/tmKUE3AOn/AKZyG+wS9BNRS0WhQHoEmMDPGsGyt/DV2Q16qonIJNYn6KXGtC/pFcByTaClmbwPv6oLZt+9oFtYpQj1w70Qd4oeLjSJFKLSTUpZk2TUkIxvGH8GSF4NfdeI24040tHCXQR4lMZ6XP3hRlCgeVSFZBq6vz8oeQvMMP+UKeWP13AnYs3V6nXrKC/CLYEXid4mGwl9szfV6k7raC+V4vCLFuglrCNObbvHYrJtB2S73AxZsx7b1pi4JIAm+7DLR12QDMQFoDbPvbRb87RKbNJbUKNaAte4RYL5oH6z89VAO6crrs0r7Djy0yzlkPv3rDCzTDHtjk5rPkdCf3Q20On9fGqGThEmyCuH+mpp5cBjsmwmTTCpMuvE16S30y6VE71HhN/2n9MAiNUA5abQY2QF/7l30BWpSQgTjp6dQAAAAASUVORK5CYII="
var imgData = Data.fromBase64(img64)
var item = new Pasteboard.Item()
item.setDataForType(imgData,TypeIdentifier.jpeg)
Pasteboard.general.clear()
Pasteboard.general.addItems([item])
Replace String on Pastebord
Here's an Omni Automation plug-in that replaces the designated string in the text currently on the global Pasteboard:
Replace String on Pasteboard
/*{
"type": "action",
"targets": ["omnifocus","omnigraffle","omnioutliner","omniplan"],
"author": "Otto Automator",
"identifier": "com.omni-automation.all.replace-string-on-pasteboard",
"version": "1.0",
"description": "This action will replace the provided string with the provided replacement string in the text currently on the user pasteboard. NOTE: the resulting text will be in plain-text format.",
"label": "Replace String on Pasteboard",
"shortLabel": "Find-Change Clipboard"
}*/
(() => {
var action = new PlugIn.Action(function(selection, sender){
// action code
var textInputField01 = new Form.Field.String(
"textInput01",
"Search for",
null
)
var textInputField02 = new Form.Field.String(
"textInput02",
"Replace with",
null
)
var checkSwitchField = new Form.Field.Checkbox(
"checkboxSwitch",
"Case-insensitive and word-only",
true
)
var inputForm = new Form()
inputForm.addField(textInputField01)
inputForm.addField(textInputField02)
inputForm.addField(checkSwitchField)
var formPrompt = "Search and replace text on the clipboard:"
var buttonTitle = "Continue"
var formPromise = inputForm.show(formPrompt,buttonTitle)
inputForm.validate = function(formObject){
var inputText01 = formObject.values['textInput01']
var inputText01Status = (!inputText01)?false:true
var inputText02Status = true
var validation = (inputText01Status && inputText02Status) ? true:false
return validation
}
formPromise.then(function(formObject){
var searchString = formObject.values['textInput01']
var replacementString = formObject.values['textInput02']
if (!replacementString){replacementString = ""}
var checkboxStatus = formObject.values['checkboxSwitch']
var contentString = Pasteboard.general.string
if (checkboxStatus){
var re = new RegExp(`\\b${searchString}\\b`, 'gi')
var newString = contentString.replace(re,replacementString)
} else {
var re = new RegExp(`${searchString}`, 'g')
var newString = contentString.replace(re,replacementString)
}
Pasteboard.general.string = newString
})
formPromise.catch(err => {
console.error("form cancelled", err.message)
})
});
action.validate = function(selection, sender){
// validation code
return (Pasteboard.general.hasStrings)
};
return action;
})();
Pasteboard Image as Attachment
The following script will add the current pasteboard image to the selected OmniFocus project or task as an attachment:
Clipboard Image as Attachment
try {
var sel = document.windows[0].selection
var selCount = sel.tasks.length + sel.projects.length
if(selCount === 1){
if (sel.tasks.length === 1){
var selectedItem = sel.tasks[0]
} else {
var selectedItem = sel.projects[0]
}
} else {
throw {
name: "Selection Issue",
message: "Please select a single project or task."
}
}
if(Pasteboard.general.hasImages){
var imageType = new TypeIdentifier("public.png")
var ext = ".png"
var data = Pasteboard.general.dataForType(imageType)
if(!data){
imageType = new TypeIdentifier("public.tiff")
ext = ".tif"
data = Pasteboard.general.dataForType(imageType)
}
if(!data){
imageType = new TypeIdentifier("public.jpeg")
ext = ".jpg"
data = Pasteboard.general.dataForType(imageType)
}
if(!data){
imageType = new TypeIdentifier("public.image")
ext = ""
data = Pasteboard.general.dataForType(imageType)
}
var fmtr = Formatter.Date.withFormat('eee-MMM-dd-yyyy-h-mm-ss-a')
var imageName = "Clip-Image_" + fmtr.stringFromDate(new Date()) + ext
var wrapper = FileWrapper.withContents(imageName, data)
selectedItem.addAttachment(wrapper)
} else {
throw {
name: "Missing Resource",
message: "There is no image on the clipboard."
}
}
}
catch(err){
new Alert(err.name, err.message).show()
}
Fetch Chart to Pasteboard
The following plug-in demonstrates how charts can be generated using an online service, and the resulting chart images be automatically placed on the Omni applicationās pasteboard (clipboard) ready to be pasted.
The URL APIs from the QuickChart website (quickchart.io) are used to generate the chart images.
You can edit this plug-in to use data from the current Omni document as the data sets for the charts, instead of the provided example data sets.
Fetch Chart Image
/*{
"type": "action",
"targets": ["omnifocus","omnigraffle","omnioutliner","omniplan"],
"author": "Otto Automator",
"identifier": "com.omni-automation.all.fetch-chart",
"version": "1.0",
"description": "This action generates a chart using the QuickChart (quickchart.io) APIs.",
"label": "Fetch Chart Image",
"shortLabel": "Fetch Chart"
}*/
(() => {
var action = new PlugIn.Action(function(selection, sender){
var menuItems = ["Doughnut","Bar","Pie","Radial Guage","Line Graph"]
var menuIndexes = new Array()
menuItems.forEach((item,index)=>{
menuIndexes.push(index)
})
var menuElement = new Form.Field.Option(
"menuElement",
"Chart Type",
menuIndexes,
menuItems,
0
)
var inputForm = new Form()
inputForm.addField(menuElement)
var formPrompt = "Fetch an image from QuickChart (quickchart.io):"
var buttonTitle = "Continue"
formPromise = inputForm.show(formPrompt,buttonTitle)
inputForm.validate = function(formObject){
return true
}
formPromise.then(function(formObject){
var menuIndex = formObject.values['menuElement']
var chosenChartType = menuItems[menuIndex]
console.log('Chosen chart: ',chosenChartType)
try {
// QuickChart https://quickchart.io/
var url = "https://quickchart.io/chart?c="
var chartDataSets = [
"{type:'doughnut',data:{labels:['January','February','March','April','May'],datasets:[{data:[50,60,70,180,190]}]},options:{plugins:{doughnutlabel:{labels:[{text:'550',font:{size:20}},{text:'total'}]}}}}",
"{type:'bar',data:{labels:['Q1','Q2','Q3','Q4'], datasets:[{label:'Users',data:[50,60,70,180]},{label:'Revenue',data:[100,200,300,400]}]}}",
"{type:'pie',data:{labels:['January','February', 'March','April', 'May'], datasets:[{data:[50,60,70,180,190]}]}}",
"{type:'radialGauge',data:{datasets:[{data:[70],backgroundColor:'green'}]}}",
"{type:'line',data:{labels:['January','February', 'March','April', 'May'], datasets:[{label:'Dogs', data: [50,60,70,180,190], fill:false,borderColor:'blue'},{label:'Cats', data:[100,200,300,400,500], fill:false,borderColor:'green'}]}}"
]
var chartDataSet = chartDataSets[menuIndex]
var url = url + encodeURIComponent(chartDataSet)
//URL.fromString(url).open()
URL.fromString(url).fetch(data => {
var item = new Pasteboard.Item()
item.setDataForType(data,TypeIdentifier.png)
Pasteboard.general.items = [item]
var alert = new Alert(chosenChartType, "Chart on pasteboard.")
alert.addOption("Done")
alert.addOption("Website")
alert.show(function(result){
if (result == 0){
console.log("Button 1")
} else {
var url = "https://quickchart.io"
URL.fromString(url).open()
}
})
})
}
catch(err){console.error(err.message)}
})
formPromise.catch(function(err){
console.error("form cancelled", err.message)
})
});
action.validate = function(selection, sender){
// validation code
// selection options: tasks, projects, folders, tags, allObjects
return true
};
return action;
})();
Doughnut:
Line:
Bar:
Pie:
Radial Guage: