SU2018 with MacOS - .plist File Access

I finally got home and tested the snippet John had in his post using SketchUp 2018 on macOS 10.13.2 High Sierra. I found three things:

  1. The code succeeded in writing a default. I could read it back during the same session.
  2. The value was stored in PrivatePreferences.json, but not until I quit SketchUp.
  3. When I restarted SketchUp I could read the default back.

There’s been a lot of further discussion since, so I don’t know whether this info is still relevant, but there it is…

1 Like

The upside with using the read_default and write_default is that I can piece meal access a particular setting or settings rather than accessing and overwriting the entire JSON file.

But you have complete control, and can be sure that settings are written when you think they should be.
SketchUp freezes and the settings changes don’t get written if you continue to rely upon the API.

Oh, and… since you’ll likely be writing your own plugin specific JSON files to a plugin specific folder, they can be broken up into smaller sets of options.

For webdialogs, stress that going the JSON route … think OpenStruct on the Ruby side, and JavaScript Object on the browser side.

Here is a screen shot of a portion of the PrivatePreferences.json file from a client running the Medeek Truss Plugin and MacOS.

The one item that jumps out at me is the vertical bar immediately after the PLUGIN_SERIAL entry. Does anyone have any idea what that is doing there?

No, but it looks like a json bug to me!

Edit: or is it maybe the cursor in the text editor when the screenshot was taken?

It’s a pipe character (if it’s not the cursor,) and does not belong there.

But I wonder what happens when in Ruby when JSON.parse() encounters an unexpected character like this. It is actually in the expected position of the next key string.

I checked with the client and his cursor was simply positioned there when the screenshot was taken, sorry for the false alarm.

2 Likes

What bugs me though is the other global settings are being read and written to just fine from all indications however when the user goes to set the serial number it doesn’t take. There must be something I am missing.

This is the piece of code that writes the new serial number:

@Pluginserial_existing = Sketchup.read_default "MEDEEK_TRUSS_PLUGIN", "PLUGIN_SERIAL"

	this_dir=File.dirname(__FILE__)
	# Fix for ruby 2.0
	if this_dir.respond_to?(:force_encoding)
		this_dir=this_dir.dup.force_encoding("UTF-8")
	end

	Sketchup.load(File.join(this_dir,"MEDEEK_UPDATE.rbs"))

	if @Pluginserial_existing
		if (@Pluginserial_existing == @Pluginserial_new)
		else
			if (@Pluginserial_new =~ /^MDKPLG1\d{2}8\d{10}$/)

				#########################################
				#
				# Register Plugin Serial Number
				#
				#########################################
				
				Medeek_Engineering_Inc_Extensions::MedeekTrussPlugin::Update::MedeekMethods.registrationlog @Pluginversion, @Pluginserial_new
			end
		end
	else
		if (@Pluginserial_new =~ /^MDKPLG1\d{2}8\d{10}$/)

			#########################################
			#
			# Register Plugin Serial Number
			#
			#########################################
				
			Medeek_Engineering_Inc_Extensions::MedeekTrussPlugin::Update::MedeekMethods.registrationlog @Pluginversion, @Pluginserial_new
		end
	end

	######


	@Pluginserial_write_result = Sketchup.write_default "MEDEEK_TRUSS_PLUGIN", "PLUGIN_SERIAL", @Pluginserial_new

Is my regexp flawed? One thing I have noticed is that when a user enters in leading or trailing white spaces it rejects the serial number so I should probably put in some code that cleans all the white spaces out of the entry first before processing it.

Just for completeness here is the registrationlog method called in the block of code above:



def registrationlog (pluginversion, pluginserial)

	@Plgversion = pluginversion
	@Plgserial = pluginserial
	@Suversion = Sketchup.version
	@Locale = Sketchup.get_locale
	@Platform = Sketchup.platform
	@Su64bit = Sketchup.is_64bit?
	@Supro = Sketchup.is_pro?

	
	@Macaddr1 = "0001"
	@Macaddr2 = "0002"


	####################
	#
	# Send Registration Data

	require 'open-uri'

	@Install_data_url = "http://design.medeek.com/calculator/sketchup/DATA/medeektrusspluginregister.pl?action=REGISTERDATA&key=jw53pt22&version=#{@Plgversion}&license=#{@Plgserial}&suversion=#{@Suversion}&locale=#{@Locale}&platform=#{@Platform}&su64bit=#{@Su64bit}&supro=#{@Supro}&macaddr1=#{@Macaddr1}&macaddr2=#{@Macaddr2}&winhostname=#{@Win_Hostname}&winphysicaladdr=#{@Win_Physicaladdress}&winadapter=#{@Win_Adapter}"
		
	begin
		open(@Install_data_url) { |io|
            		url_response = io.read
				
			if /Status: OK/.match(url_response)
				# Data transmitted successfully
				# UI.messagebox ("Registration with Medeek Server was successful, Click OK to proceed.")
				UI.messagebox("Thank-you for registering the Medeek Truss Plugin.\nYour licensed copy is registered with Serial Number: #{@Plgserial}") 	
			else
				UI.messagebox ("Registration with Medeek server was unsuccessful, Click OK to proceed.")	
			end	
					
          	}

       	rescue StandardError => e
         	UI.messagebox ("Unable to connect to Medeek server for registration, Click OK to proceed.")
        end

end

To detect a match that has possible whitespace … use the \s along with the “0 or more” asterisk modifier.

SERIALTEST = /\A\s*MDKPLG1\d{2}8\d{10}\s*\z/

Since it is a string and not a line, I use \A instead of ^, and \z instead of $.
But I suppose it’s personal preference. This also works …

SERIALTEST = /^\s*MDKPLG1\d{2}8\d{10}\s*$/

So …

if @Pluginserial_new =~ SERIALTEST

To clean up the actual whitespace …

@Pluginserial_new.strip!

or to test stripped but not change the string …

if @Pluginserial_new.strip =~ /^MDKPLG1\d{2}8\d{10}$/

Another thing I notice is that there are 3 conditions that fall through your conditional that do not do anything, nor output an indicator.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.