Merlin: Applescript splitting activity

From time to time we get asked whether it is possible to split an activity in Merlin so it can be assigned to various resources at different time periods. Well, you will not find an explicit function doing so, but if you like, you may use the following AppleScript.

If you save it under ~/Library/Application Support/Merlin/SendToMenu it will always be accessible in Merlin over the File > Send To submenu.

(*     Scripting with Merlin 2

    You may incorporate this ProjectWizards sample code into your program(s) without
    restriction.  This ProjectWizards sample code has been provided "AS IS" and the
    responsibility for its operation is yours.  You are not permitted to
    redistribute this ProjectWizards sample code as "ProjectWizards sample code" after having
    made changes.  If you're going to redistribute the code, we require
    that you make it clear that the code was descended from ProjectWizards sample
    code, but that you've made changes.

    Copyright ® 2009-2014 ProjectWizards, Melle, Germany. All rights reserved.

    This script can split an activity into two. Keeps assignments and can enter lead or lag between them.
    Version 1.0
        Written by Vicky Stamatopoulou
    For ProjectWizards
    Nov 4, 2009

    version 1.1
    changed on May 11, 2011
    changed by Vicky Stamatopoulou
    info: added more error checks

        version 1.2
        changed on May 23, 2011
        changed by Vicky Stamatopoulou
        info: solved an issue with assignments, completion percentage, and actual start date. 
    
    Version info: 1.3
    Author: Vicky Stamatopoulou
    Date: June 2014
    Info: the script will now run on Mavericks too, revised the error and error reporting handing
    It does not handle completed or activities in progress. 
    

*)
----

property howthisworksDialogString : "Please select the activity you would you like to split it into 2 and restart this script."
property TheActivityDialogString : "The activity "
property isPlannedForDialogString : " is planned for "
property splitinto2DialogString : ". Would you like to split it into 2 subtasks?"
property howMuchLeadDialogString : "How much lead/lag in days would you need? Leave entry blank for zero lead."
property errorDialogString : "This script splits activities in plan mode having a given work. It cannot split activity groups, assignments, milestones, elements, completed or started activities."
property completedDialogueString : "Nothing to split! Activity already completed. "
property notStartedDialogueString : "Nothing to split! Activity is in progress. "
property noWorkDialogueString : "Nothing to split! Activity has no given work. "
property milestoneDialogueString : "Nothing to split! Milestone selected. "
property assignmentDialogueString : "Nothing to split! Assignment selected. "
property projectDialogueString : "Nothing to split! Project row selected. "
property groupDialogueString : "Nothing to split! Group row selected. "

global myStrings
set myStrings to {howthisworksDialogString, projectDialogueString, groupDialogueString, assignmentDialogueString, milestoneDialogueString, notStartedDialogueString, completedDialogueString, noWorkDialogueString}


on reportError(errorNumber)
    global myStrings
    -- errors: 
    -- 1 nothing selected
    -- 2 for project
    -- 3 group
    -- 4 assignment
    -- 5 milestone
    -- 6 in progress
    -- 7 completed
    -- 8 no work
    
    activate
    set displayMessage to ""
    set displayMessage to item (errorNumber) of myStrings
    
    tell application "Merlin" to display dialog displayMessage & return & return & errorDialogString buttons {"OK"} default button 1 with icon 0
    
    
end reportError

on checkToRun(anItem)
    global errorStatus
    -- errors: 
    -- 1 no selection
    -- 2 for project
    -- 3 group
    -- 4 assignment
    -- 5 milestone
    -- 6 in progress
    -- 7 completed
    -- 8 no work
    
    tell application "Merlin"
        
        if (class of anItem is project) then
            set errorStatus to 2
            return false
        end if
        if (is milestone of anItem is true) then
            set errorStatus to 5
            return false
        end if
        
        -- do not handle zero work and zero duration
        set TheWork to given planned work of anItem
        if TheWork is missing value then
            set errorStatus to 3
            return false
        end if
        
        if (class of anItem is assignment) then
            set errorStatus to 4
            return false
        end if
        -- check groups
        try
            assignments of anItem
        on error
            set errorStatus to 3
            return false
        end try
        
        
        
        -- do not handle completed tasks or not yet started ones
        set theCompleteness to given actual completion of anItem
        if theCompleteness is missing value then set theCompleteness to actual completion of anItem
        
        if (theCompleteness is 1) or (theCompleteness is greater than 0) then
            
            
            if theCompleteness is greater than 0 then set errorStatus to 6
            if theCompleteness is 1 then set errorStatus to 7
            
        end if
        
        
        if errorStatus > 0 then
            return false
        else
            return true
        end if
    end tell
end checkToRun


tell application "Merlin"
    
    activate
    set errorStatus to 0
    try
        -- get selection
        set myselection to selected object of main window of document 1 as specifier
        
    on error
        set errorStatus to 1
    end try
    -- get activity information
    
    if errorStatus is 0 and checkToRun(myselection) of me then
        
        tell myselection
            set TheActivity to title
            set TheWork to given planned work
            set TheExpectedWork to expected work
            
        end tell
        
        
        try
            -- get assignement information
            set TheResource to assigned resource of myselection as list
            set TheResourceName to (names of TheResource) as list
            set TheActualStart to actual start date of myselection
        end try
        
        tell TheWork
            set TheAmount to amount
            set TheFloating to floating as boolean
            set TheRelativeError to relative error as integer
            set TheUnit to unit
            
        end tell
        
        try
            set dialogResult to display dialog TheActivityDialogString & TheActivity & isPlannedForDialogString & TheAmount & " " & TheUnit & splitinto2DialogString buttons {"Cancel", "OK"} default button "OK" cancel button "Cancel" with icon 2
            
            
            if button returned of dialogResult is "OK" then
                
                set workForSubTasks to TheAmount / 2
                set TheUnit to unit of TheWork
                
                -- Prompt for lead/lag
                set TheLead to display dialog howMuchLeadDialogString default answer "" buttons {"OK"} default button 1 with icon 2
                
                set TheLead to text returned of TheLead
                if TheLead is "" then set TheLead to "0d"
                
                -- create first task
                set TheNewOne to (make new activity) as specifier
                tell TheNewOne
                    set given planned work to {amount:workForSubTasks, unit:TheUnit, floating:TheFloating, relative error:TheRelativeError}
                    set title to "part 1"
                    
                    
                    
                end tell
                
                
                -- create second task
                set TheNewSec to (make new activity) as specifier
                tell TheNewSec
                    set given planned work to {amount:workForSubTasks, unit:TheUnit}
                    set title to "part 2"
                end tell
                
                -- relate activities finish to start
                set TheRelation to relate TheNewOne to TheNewSec
                -- set lead/lag onto linkage
                set buffer duration of TheRelation to TheLead
                repeat with aRes in TheResource
                    tell aRes
                        -- do assignements
                        try
                            assign resource to activity TheNewOne
                            assign resource to activity TheNewSec
                        end try
                    end tell
                end repeat
                
                -- group the activities under the selection
                move TheNewOne to the end of every activity of myselection
                move TheNewSec to the end of every activity of myselection
                tell myselection
                    set given planned work to {amount:0, unit:TheUnit, floating:TheFloating, relative error:TheRelativeError}
                end tell
            end if
        on error number -128
            --don't do anything        
        end try
    else
        reportError(errorStatus) of me
    end if
    
end tell


You can find the script compiled and zipped here.

Post information
originally posted: Nov 4, 2009
last updated: June 6, 2014

17 thoughts on “Merlin: Applescript splitting activity

  1. “Expected end of line, etc. but found identifier.”

    I get this error whether or not I remove the ‘¬’.

    From the Note above, I am not sure exactly what I am supposed to do to avoid the error..

    It happens in the following line at the beginning:

    set myselection to selected object of ¬

    on the word “object”.

    thanks,
    Andrew

  2. Hi Andrew,

    I have created an Applescript out of the above lines and removed the ‘¬’ sings. You can find it compiled and zipped here

    Best regard, Vicky

  3. Vicky, I appreciate the script, it does what it claims. I have a need to split a specific task over the duration of the project but show that split and distribution in my Gantt chart. In MS Project I used to set the task and then split the duration indicator in the Gantt chart directly to show the distribution. It is the same task by the same resource, but they are only dedicating a specific time period each week for this task (which is billed accordingly). How can I do this in Merlin2?

  4. Also would like to know how to split a task – ms projects and open project I seen to remember you simply right click and select ‘split’. This creates a pause in the task which you can adjust graphically on the gantt chart

  5. Ric and Alex… this is not implemented in Merlin but forwarded as feature request to the management.

  6. Hi Vicky,
    your script to split a task in 2 with a lag is a real improvement and will help a lot on my side.

    Anyway, it would be needed that the split keeps resource assigned and actual inspector pan completed as it was before the split.
    In addition to this, it would be perfect to be able to split in two parts a task as follow:
    – originally planned for a 3 weeks task split in 2 tasks of 1,5 week.
    –> Would it be possible to choose lenght of part1?
    For example, 1 week and by default second part would be 2 weeks.

  7. The script currently splits the task into two half sized tasks.
    But sure, you may change it, to display a dialogue asking for the size of the first part and let the script calculate the remaining task.

    I have noticed however, there is a problem with the assignments, sometimes they won’t get transferred. I have modified the script to solve it.

    Also given actual completion is now taken under consideration. For a completion % of 100 %, both sub tasks are completed by 100%.
    By a completion of 50 %, the first sub task gets 100%
    A completion of 45 %, would fill first sub task by 90 % so that the overall completion remains 45%.
    A completion of 90 % would fill first sub task by 100% and second activity by 80 %, so that overall completion remains 90%.

    Best regards, Vicky

  8. Hello,
    thanks for your answer.
    Where can I find script with latest corrections implemented?

    You description about changes added are exactly what I need.
    And as I am not a script writer, I prefer to lean on your skill.

    Best regards, Alex Collin

  9. I have updated the blog post to contain the latest version of the script which considers completion % and assigned resources…
    So you may just copy it out of this blog post, paste in a new applescript script, compile and use it.

    Best regards, Vicky

  10. Hi Vicky

    Many thanks for the script, I sent Merlin support a mail, last week asking about split activities to which they replied “no support for this functionality” … imagine my surprise to happen upon your script which works precisely as I need it to!! Let Support know?? I was about to look for another tool but now it seems Merlin might just do the trick!!

  11. Hi Mitch,

    sure. Support knows of this applescript, which splits the activities by creating sub-activities. They don’t directly recommend it to all users asking for splitting activities, as it is not a standard function in Merlin (some users don’t like using applescripts) and the script cannot create the interruptions all in one line (this is not possible in the current structure of Merlin projects).

    Really great that you find this script useful 🙂

    Best regards, Vicky

  12. Huh… just noticed. The script used to not run on 10.9.4
    So I’ve changed it and improved its error handling.
    Best regards, Vicky

    It handles now only tasks in plan mode (not such in progress, or completed). Should you need to split into completed and remaining part, please check this script

  13. Hi Vicky
    I am getting the following error when running the script

    error “Merlin got an error: Can’t make missing value into type date.” number -1700 from missing value to date

    Any ideas?

    Thanks for your time

  14. I think I know…funny though that I hadn’t this error while I’ve checked the script.
    Please find the line
    set actual start date to TheActualStart

    and comment as
    — set actual start date to TheActualStart

    Which is your OS X version?

    Best regards, Vicky

  15. Hello again Darren, I’ve modified the script in the blog and in the zip archive. If you like you can repeat the download. It should work now and will not bring the date error anymore.

    Best regards, Vicky

  16. Is there a script for Merlin 3?

    Just clarification, would this script allow access to split an activity over one resource during the course of the total duration. For example, if we had a Resource A assigned to Activity A for 100% between February and March and then in April the same Resource and the same Activity were paired although the Resource units went down to 50% – is this able to be communicated along the one line. Without having to add in further activities, when really it is the same activity, just at a smaller resource level.

    Thanks,
    Rebecca

Comments are closed.