Hope that I don't spoil you fun of figuring it out yourself, I'm not even sure if my approach was the right one, but here it goes:
First of all, you DON'T have to go back to ActionViewController. I just pasted the lines that are needed to execute the script, straight to TableViewController:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = NSExtensionItem()
let argument: NSDictionary = ["customJavaScript": tableScripts[indexPath.row].tableScript]
let webDictionary: NSDictionary = [NSExtensionJavaScriptFinalizeArgumentKey: argument]
let customJavaScript = NSItemProvider(item: webDictionary, typeIdentifier: kUTTypePropertyList as String)
item.attachments = [customJavaScript]
extensionContext?.completeRequest(returningItems: [item])
}
You might think what tableScripts[indexPath.row].tableScript
in the line 3 is. Well, it's from the custom class I made to store the written scripts along with their names:
class scriptSavedForTableView: NSObject, Codable {
var tableName: String?
var tableScript: String?
init (tableName: String, tableScript: String) {
self.tableName = tableName
self.tableScript = tableScript
}
}
tableScripts
then is just a var
of [scriptSavedForTableView]
.
So, having a script saved to a table view, when you click a row, the view controller is automatically dismissed and the script is executed.