Jump to content

A way to set mods in JSON that works with AppleScript records


Pete31
 Share

Recommended Posts

24 minutes ago, deanishe said:

How are you generating the JSON because none of the AppleScript libraries I've seen can reliably produce valid JSON?

 

No library involved, just AppleScriptObjC.

 

-- Read & Write Write JSON in AppleScriptObjC

use AppleScript version "2.7"
use scripting additions
use framework "Foundation"

property maxResults : 500
property theJSONPath : "/Users/USER/Desktop/demo.json"
property theIconPath : "/Applications/DEVONthink 3.app/Contents/Resources/DEVONthink 3.icns/"
property NSJSONWritingPrettyPrinted : a reference to 1

on run (argv)
	try
		set theQuery to (item 1 of argv)
		
		set newRecord to {title:theQuery, arg:theQuery, variables:{scope:"", modifyScope:"false", modifyQueryWith:""} ¬
			, mods:¬
			{ctrl:{arg:theQuery, subtitle:"scope:", variables:{scope:"", modifyScope:"true", modifyQueryWith:""}} ¬
				, alt:{arg:theQuery, subtitle:"Anführungszeichen", variables:{scope:"", modifyScope:"false", modifyQueryWith:"quotes"}} ¬
				, cmd:{arg:theQuery, subtitle:"scope:selection", variables:{scope:"selection", modifyScope:"true", modifyQueryWith:""}} ¬
				, shift:{arg:theQuery, subtitle:"Asterisks", variables:{scope:"", modifyScope:"false", modifyQueryWith:"asterisks"}} ¬
				} ¬
				} as record
		
		set theJSONFile to POSIX file theJSONPath
		if (get eof theJSONFile) ≠ 0 then
			
			-- Read JSON
			
			set oldJSON_data to current application's NSData's dataWithContentsOfFile:(theJSONPath)
			set {oldJSON, theError} to current application's NSJSONSerialization's JSONObjectWithData:oldJSON_data options:0 |error|:(reference)
			if oldJSON = missing value then error (theError's localizedDescription()) as text
			if oldJSON's isKindOfClass:(current application's NSDictionary) then
				set oldJSON_AS to {oldJSON as record}
			else
				set oldJSON_AS to oldJSON as list
			end if
			
			set newJSON_AS to {newRecord} & oldJSON_AS
		else
			set newJSON_AS to {newRecord}
		end if
		
		-- Write JSON
		
		set theJSONData to current application's NSJSONSerialization's dataWithJSONObject:newJSON_AS options:NSJSONWritingPrettyPrinted |error|:(missing value)
		theJSONData's writeToFile:(theJSONPath's POSIX path) atomically:false
		set JSONStr to (current application's NSString's alloc()'s initWithData:theJSONData encoding:(current application's NSUTF8StringEncoding)) as text -- testing
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "Error" message error_message as warning
		return
	end try
end run

 

 

Link to comment
Share on other sites

15 minutes ago, Pete31 said:

No library involved

 

There’s always a library involved. You’re using Foundation.


In that case, you can construct your feedback using NSMutableDictionary instead of AppleScript records. Then you can use setObject:forKey:, which supports all valid JSON keys (unlike AppleScript records).

Link to comment
Share on other sites

1 hour ago, deanishe said:

In that case, you can construct your feedback using NSMutableDictionary instead of AppleScript records. Then you can use setObject:forKey:, which supports all valid JSON keys (unlike AppleScript records).

 

Thank you very much for looking into this deanishe! No idea why I didn't try to do it in AppleScriptObjC. However I've found an even better way, no idea why I didn't think of this before, though.

 

It's actually possible to use any string we want as a key in an AppleScript record, we just have to use pipes 🙂

 

set newRecord to {title:theQuery, arg:theQuery, variables:{scope:"", modifyScope:"false", modifyQueryWith:""} ¬
	, mods:¬
	{ctrl:{arg:theQuery, subtitle:"scope:", variables:{scope:"", modifyScope:"true", modifyQueryWith:""}} ¬
		, alt:{arg:theQuery, subtitle:"Anführungszeichen", variables:{scope:"", modifyScope:"false", modifyQueryWith:"quotes"}} ¬
		, cmd:{arg:theQuery, subtitle:"scope:selection", variables:{scope:"selection", modifyScope:"true", modifyQueryWith:""}} ¬
		, shift:{arg:theQuery, subtitle:"Asterisks", variables:{scope:"", modifyScope:"false", modifyQueryWith:"asterisks"}} ¬
		, |ctrl+shift|:{arg:theQuery, subtitle:"CTRL + SHIFT", variables:{scope:"", modifyScope:"false", modifyQueryWith:"asterisks"}} ¬
		, |alt+shift|:{arg:theQuery, subtitle:"ALT + SHIFT", variables:{scope:"", modifyScope:"false", modifyQueryWith:"asterisks"}} ¬
		, |cmd+shift|:{arg:theQuery, subtitle:"CMD + SHIFT", variables:{scope:"", modifyScope:"false", modifyQueryWith:"asterisks"}} ¬
		} ¬
		} as record

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...