Category Archives: Code

View your core data SQLite database

I am currently writing an ios app that uses a medium size pre-populated core data database. Having worked with mysql previously along side php and java I have to say that the way core data is implemented makes it very powerful and great to use for object persistence.
In my app I am opening a plist file as a dictionary and looping through each entry to create the core database within the app.
I am also using a UITableView and a NSFetchedResultsController to display the table in the app which is another strength of core data as it automatically reloads the table when the data changes.
The first time I ran the app and viewed the table some of the objects had the wrong section  headers and also some of the section titles were incorrect.
When I checked the plist files the entires that were showing up incorrectly were fine in the plist file.  In mysql I would have just done a select * to see all the entires in the table to see what the database looked like but wasn’t sure how to do this with core data.

This is what I ended up doing to find the incorrect entities:

I set the SQL debug flag in xcode. In xcode 4 this can be done from the Product menu by selecting “Manage Schemes” and then editing the current scheme you are using. Select the “Run “phase and then select the “Arguments” tab. Here you will see options to set arguments and environment variables. Add the code below as an argument:

-com.apple.CoreData.SQLDebug 1

I then ran my app in the ios simulator which gave me the location of the sql file on my computer. I copied the location (minus the sql file name) and pasted it into the “Go to Folder” field in the “Go” menu of finder then copied the .sql file into my user folder.

I then downloaded SQLite Database Browser which is a free open source SQLite browser (the title is pretty explanatory really) and opened my .sql file and viewed the tables.

I found the entires which were incorrectly entered and fixed them in my .plist file that populates my core data database which in tern fixed the problem with the section headers I was having. All without too many (more) hairs turning grey!

Play a random sample Kontakt with scripting

Over the last year or so I have been working on creating a sample based software instrument in Native Instruments’ Kontakt, which has its own scripting language Kontakt Script Processing.  I had to learn the language from scratch but found the KSP reference  pdf  bundled with Kontakt and information on the Kontakt scripting forums at the Native Instruments website great resources.

The instrument I am currently working on is now over 3000 lines of code and I have started dreaming of $EVENT_NOTE variables but one of the first requirements was to trigger a sample from a range of samples randomly. This was pretty easy to do but because it was random it often played the same sample twice in a row. I then added a couple of extra lines to make sure that it didn’t play the same notes twice. I thought I would upload this code as it may prove useful to people who are leaning KSP.  It also shows how to create and call functions and declare variables.

 

on init
       
    {declare variables to hold previous values}
 declare polyphonic $random
 declare $last1 := 0
 declare $last2 := 0

 
 
declare $lastTime := 0
declare polyphonic $velocity := 1
    
    
{Declare keys for Random Triggers ie the note that will trigger the random sample  }
 
declare $key_trigger := 108
   
{declare the range of the samples that will be triggered randomly}    
    
declare $lower_limit := 0
declare $upper_limit := 23    
    
    
{Set key colours}

set_key_color($key_trigger, $KEY_COLOR_RED)     
set_key_color($lower_limit, $KEY_COLOR_GREEN)
set_key_color($upper_limit, $KEY_COLOR_GREEN)   
    
    
end on

{Random sample Function}


function playRandomSample
    
     
            
            $random := (random($lower_limit, $upper_limit))
        
           
        {check to see if the next step has been used in the last two }
                
                while ($random = $last1 or $random = $last2)
               
                $random := (random($lower_limit, $upper_limit))
            
                end while
                
                play_note ($random, $velocity, 0, 0)
  
        
            $last2 := $last1
            $last1 := $random
       
   end function

on note
  if ($EVENT_NOTE = $key_trigger)
      
      $velocity := $EVENT_VELOCITY
      
      call playRandomSample
      
      end if  
    
    
    
    
end on

 

feel free to leave any questions/comments below

 

 

 

Chronosync backup when drive plugged in with applescript

Firstly excuse the extremely uncreative title of this post but I have been working all day and my creativity levels are a little on the uncreative side at this stage of the day.
At the studios we have couple of different backup systems running to DLT and LTO tapes plus some offsite drives that we use for disaster recovery. After struggling with massive issues with the latest version of retrospect for mac and the horrible support we decided to move to Chronosync which has been fantastic.
One thing that we wanted to do was automatically run a backup when we connected a particular offsite backup drive. I managed to get this to work by running an applescript in automater whenever a new drive was added using folder actions.

When new drives are added to OSX they are mounted in the /Volumes folder which you can’t normally access in finder. To get around this you can create a symlink in the terminal by typing:

ln -s /Volumes/ ~/vol-link

this will create a link named “vol-link” in your home folder that points to the Volumes folder.

Then open automater in the applications folder and chose “folder action” from the start up screen

On the top you can choose the folder that will perform an action if a file or folder is added to it.
In here choose your vol-link.

Drag across the “Run AppleScript” action form the left and then add the AppleScript code from below

Note that you will have to change the volume names and path of your Chronosync container document and you can modify this script to work with more drives or less.

Save your workflow and that’s it.

We also have our backups stored in an encrypted disk image so have set the last document in the container to eject the image when it’s finished. The script then ejects the drive

Any questions or comments let me know

set VolumeName1 to "OffsiteBackUP1"
set VolumeName2 to "OffsiteBackUP2"

if volumeMounted(VolumeName1) = 1 then
	try
		tell application "ChronoSync"
			activate
			
			open alias "HD RAID:Users:serverhomedir:Documents:Offsite Backup 2.csyn"
			
			tell document 1 to Synchronize
			
			--hold on 'till the sync is complete
			
			repeat while syncStatus of document 1 is not 0
			end repeat
			
			tell document 1 to save
			tell document 1 to close
			
			
			
			
		end tell
	end try
	
	delay (5)
	
	tell application "Finder"
		activate
		eject disk VolumeName1
		
	end tell
	
	
else
	
	if volumeMounted(VolumeName2) = 1 then
		
		
		
		try
			tell application "ChronoSync"
				activate
				
				open alias "HD RAID:Users:serverhomedir:Documents:Chronosync Offsite Scripts:Offsite Backups.csyn"
				
				tell document 1 to Synchronize
				
				--hold on 'till the sync is complete
				repeat while syncStatus of document 1 is not 0
				end repeat
				
				tell document 1 to save
				tell document 1 to close
				
				
				
				
			end tell
		end try
		
		delay (5)
		
		tell application "Finder"
			activate
			eject disk "VolumeName2"
			
		end tell
		
	end if
	
end if

on volumeMounted(VolumeName)
	tell application "Finder"
		if (exists disk VolumeName) then
			return 1
		else
			return 0
		end if
	end tell
end volumeMounted

Prepare for Segue in IOS 5

I’ve been working on a few iPhone and iPad apps using IOS 5 and have been watching Paul Hegarty’s iPhone course at Stanford though iTunes U. I recommend watching it for anyone who is interested in IOS development as it’s as fantastic source of information and Paul is a great teacher.

One of the many useful things I found when first delving into the model view controller paradigm was the method that is called just before a Segue to another view controller prepareForSegue:

You can use this to set properties in the view controller you are about to segue to such as the delegate which is very useful.
You just need to set the identity of the segue in Xcode so it can be identified.

Here is a small snippet based on Paul’s example

-(void) prepareForSegue:(UIStoryboardPopoverSegue *)segue sender:(id)sender
{
    if ([segue.identifier hasPrefix:@"Next View"]) {
      
        //The segue is to the a View Controller called NextViewController
        (NextViewController *nvc =  (NextViewController  *) segue.destinationViewController;
       
       //Set properties in the view controller - note the views outlets are not set yet
        nvc.stringProperty1 = @"This Is Property 1";
      
         nvc.property2 = @"This Is Property 2";
       //Set self as the delegate
       nvc.delegate = self;
    }
}

VNC Connect Applescript

At the studio we run a Mac based network with multiple servers and clients. With remote desktop enabled on the Macs or a VNC server (such as Tight VNC) on Windows it’s easy to view and control the screens of remote systems. Which is pretty handy when most of your systems are located in a distant machine room and don’t want to get off your chair.
While it’s pretty easy to select the computer in the finder and press screen share, I decided to write an Applescript to streamline the process of frequently connecting to same remote systems and thought I would share it in case anyone finds it useful.

The remote systems will need to have a static IP address and this script will only work with OSX 10.5 and up.
To customise the script all you need to do is change the values in the lists and ensure that the name and address values keep their relative position in the list. You can also add more than three systems by just adding to lists.

 

(* VNC Connect script v1 www.sandymilne.com *)

on get_pos(aCompterName, aList)
	repeat with theItem from 1 to the count of aList
		if item theItem of aList is aCompterName then return theItem
	end repeat
	return 0
end get_pos

(* Set computer address and Names *)

set computerNameList to {"Server", "Windoze", "iMac 1"}
set computerAddressList to {"10.0.0.8", "10.0.0.6", "192.168.1.22"}


set selectedHostName to choose from list computerNameList with prompt "Select a host:"

if selectedHostName is not equal to false then
	set connectwith to item (get_pos((selectedHostName as string), computerNameList)) of the computerAddressList
	
	do shell script "open -W vnc://" & connectwith
end if



 

You can download the script here:
VNC Connect.zip