The Omni Group
These forums are now read-only. Please visit our new forums to participate in discussion. A new account will be required to post in the new forums. For more info on the switch, see this post. Thank you!

Go Back   The Omni Group Forums > Developer > AppleScripting Omni Apps
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
setting Text properties using Ruby appscript or RubyOSA Thread Tools Search this Thread Display Modes
So here is some sample Ruby Appscript for OmniGraffle, for those who are interested. I did an application in AppleScript which take data from Numbers and builds a diagram in OmniGraffle. The problem was it was too slow to be useful. (I mean, I let it run over night and it was still chugging away in the morning.) So, I switched to Ruby and Appscript. I'm running into some new problems, that I can't seem to sort out. If anybody can help that would be great. I will post additional questions on this thread as well as success.

The one thing I'm trying to get right is how to set the size of the text and the alignment of the text. Below is my code...

Code:
begin; require 'rubygems'; rescue LoadError; end

require "appscript"
include Appscript

# assumes the document specified is open and the canvas is named as specified
_DocumentName = "Sample Scripting Diagram.graffle"
_CanvasName = "Ruby Test Canvas"

=begin
--This is the AppleScript of the two shapes...
tell application "OmniGraffle Professional"
	tell canvas of front window
		make new shape at end of graphics with properties {name: "Circle", textSize: {0.800000, 0.700000}, text placement: top, fill: no fill, textPosition: {0.100000, 0.150000}, origin: {75.000000, 194.857147}, autosizing: vertically only, size: {250.000000, 34.285713}, text: {text: "This Shape #1", font: "Helvetica-Bold", text: "This Shape #1"}, draws shadow: false}
		make new shape at end of graphics with properties {text placement: top, fill: no fill, origin: {100.000000, 400.000000}, autosizing: vertically only, size: {200.000000, 24.000000}, text: {text: "This Shape #2", text: "This Shape #2"}, draws shadow: false}
	end tell
end tell
=end

  puts "Working with canvas: #{_CanvasName} of document #{_DocumentName}"
  #puts app('OmniGraffle Professional 5').documents.name.get # => prints out the names of all the documents (looks like it includes templates, too)
  
  workingCanvas = app('OmniGraffle Professional 5').documents[_DocumentName].canvases[_CanvasName]  # gets the canvas we want to work with.
  #Create a new shape without specifying propterties
  shapeOne = workingCanvas.graphics.end.make(:new => :shape)
  # Now I go down the list of properties and try to set them
  shapeOne.origin.set([100 , 200])
  shapeOne.size.set([200 , 75])
  shapeOne.text_placement.set(:top) # I've had some incosistant results with using 'top' in single quotes. However, the :top seems to work always
  shapeOne.text.set("This Shape #1")
  shapeOne.text.font.set("Helvetica-Bold")
	shapeOne.fill.set(:no_fill) # I tried putting 'no fill' instead of :no_fill and it doesn't seem to work. The result is fill: 0
	shapeOne.draws_stroke.set(true)
	shapeOne.autosizing.set(:vertically_only)
	shapeOne.name.set('Circle')
	shapeOne.draws_shadow.set(false)
  #thisNewShape.text.alignment.set(:center) # This doesn't work and I can't seem to figure out how to set the alignment of the text
  #thisNewShape.text.size.set([10]) # This doesn't work and I can't seem to figure out how to set the size of the text
	#thisNewShape.vertical_padding.set([0])
	#thisNewShape.side_padding.set([0])

  shapeTwo = workingCanvas.graphics.end.make(:new => :shape , :with_properties => { :origin => [100 ,  400] , :size => [200 , 75], :text_placement => :top , :text => "This Shape #2" , :fill => :no_fill ,:draws_stroke => true ,  :autosizing => :vertically_only , :name => 'Rectangle', :draws_shadow => false})
 
So, I still need some help... I need to be able to get the user data items for a shape. From the below info, you will see that I figured out how to set the user data. But it's useless to me unless I can query for it and compare it to known values.

Here is what I've come across... There is a cool utility into which you can paste AppleScript and generate the corresponding code in either Python, Ruby, or ObjC.
The utility is ASTranslate and can be found at http://appscript.sourceforge.net/tools.html#astranslate

Just a sample of what is did for me... I selected the shape that I'd like to recreate and copy as AppleScript..

This copies the following apple script...
-----------------------------------
Code:
tell application "OmniGraffle Professional"
	tell canvas of front window
		make new shape at end of graphics with properties {user data: {hostName: "localhost"}, text placement: top, fill: no fill, origin: {121.000000, 354.500000}, autosizing: vertically only, size: {200.000000, 24.000000}, text: {text: "This Shape #2", text: "This Shape #2"}, draws shadow: false}
	end tell
end tell
-----------------------------------

Paste this code in ASTranslator and translate a Ruby to get..
---------------------
Code:
app("OmniGraffle Professional 5").windows[1].canvas.make(:at => app.windows[1].canvas.graphics.end, :new => :shape, :with_properties => {
    :origin => [121.000000, 354.500000],
    :text => {
                 :text => "This Shape #2"
             },
    :text_placement => :top,
    :draws_shadow => false,
    :user_data => {
                      "hostname" => "localhost"
                  },
    :autosizing => :vertically_only,
    :fill => :no_fill,
    :size => [200.000000, 24.000000]
})
--------------------

I still can't seem to figure out how to set user data after the object is created.
And I still need to figure out:
  1. How to update the user_data_item value.
  2. How to get the user_data_item name and value. This is needed so I can identify shapes with specific user data values.
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting AppleScript variable to text of a cell Patrick J OmniOutliner 3 for Mac 2 2011-12-03 04:31 PM
Text Editing in Preferences not setting Jolt21 OmniGraffle General 1 2011-04-08 10:21 AM
Default text align setting enfact OmniGraffle General 3 2010-09-14 07:26 AM
Export Properties:Notes as HTML Roll Over Text? jeffreykennedy OmniGraffle General 6 2009-10-26 08:40 AM
RubyOSA newtonapple OmniFocus Extras 2 2007-10-16 09:08 PM


All times are GMT -8. The time now is 12:34 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.