Lessons OnOpenFile ;***************************** ; OnOpenFile ;***************************** ;------------------------------ ; Script Name: CopyMarkers.bbs ; Macro: OnOpenFile ; Created 1 July 2010 ; Revision: 1, 8 July 2010 ; Biopac Systems, Inc. ;------------------------------ ; choose file that has markers to copy from one channel to another ; choose a directory also ; restore path for later resinstatement GetCurrentPath P$ ; choose directory ChooseDirectoryPrompt "Go to the directory that has the graph file of interest...", A$,z if z = 1 Warning Show "User selected Cancel, Program terminating.", 1 Halt endif ; Change working directory to directory where file resides Warning Show "Setting directory path to new working directory...", 1 Set Folder Path A$ ;open the file Open "" a, F$, A$ if a = 3 Warning Show "Error in file selection. Program terminating.", 1 Halt endif ; Have user select channels that have markers and one to transfer them to ChooseChannel "Enter the channel number that contains the markers", s if s = -1 Warning Show "User selected Cancel, exiting the program.", 1 goto LoopOut endif ChooseChannel "Enter the channel number that you wish to write markers to.", c if c = -1 Warning Show "User selected Cancel, exiting the program.", 1 goto LoopOut endif ;Enable the target channel for the markers Set Channel c,On ;Get the channel length Get Channel Length c,L ;Enable the channel to be scanned Set Channel s,On ;Get the channel length Get Channel Length s,K if K <> L Warning Show "Source and target channels are not the same length. Program terminating", 2 goto LoopOut endif Get Marker Count b ;set counter and initial marker index i = 1 // search index m = 0 // marker index q = 0 // the index for the number of marker indices ;Invoke a completion themometer Transform AsyncProgress Show Transform AsyncProgress SetMessage "Searching for markers..." ;----------------------------------------------------------------------------------------------------- ; Find the markers (non-zero string) Loop2: //<------------ Top of Processing Loop ---------------------------------------------- ;Search for marker with user inpu criteria SearchForMarker "", s, i, m //<-- search chan s starting at index i looking for marker index m if m <> 0 q = q+1 //<-- increment marker index, will be used to help update the Insert themometer Get Marker Type m,T$ //<- Marker processing Get Marker Time m,t //<- Marker processing Array "Indices" Set q, m StringArray "Codes" Set m, T$ Array "Times" Set m, t p = m/b*100 //<- Update themometer parameter Transform AsyncProgress SetPercentComplete p //<- Update progress themometer Transform IsCanceled x if x = 1 Warning Show "User selected Cancel, exiting the program.", 1 Transform AsyncProgress Hide goto LoopOut endif i = m+1 //<-- start new search, "leapfrog" over the found marker index m = 0 //<-- reset the marker index parameter if i < b+1 goto Loop2 //<-- Bottom of Processing Loop after finding and placing markers else Warning Show "All done copying markers",2 Transform AsyncProgress Hide goto JumpOut //<-- All done endif else Transform IsCanceled x if x = 1 Warning Show "User selected Cancel, exiting the program.", 1 Transform AsyncProgress Hide goto LoopOut endif i = i+1 //<-- Marker not found on last i, increment and search again m = 0 //<-- Reset the marker index parameter if i < b+1 //<-- Reach end of marker limit? goto Loop2 else Warning Show "No more markers found to copy",2 Transform AsyncProgress Hide goto JumpOut endif endif JumpOut: if q<1 Warning Show "No markers found on the specified channel",2 goto LoopOut endif Transform AsyncProgress Hide ;Present a themometer to show how long it will take to insert the markers into the target channel Transform AsyncProgress Show Transform AsyncProgress SetMessage "Inserting the markers..." k = 1 Loop1: // Writing the markers into the target channel Transform IsCanceled x if x = 1 Warning Show "User selected Cancel, exiting the program.", 1 Transform AsyncProgress Hide goto LoopOut endif if k < q+1 Array "Indices" Get k, m Array "Times" Get m, y StringArray "Codes" Get m, W$ Set HCursor y,y InsertMarker "", c, W$ //<-- Actually copying markers into target channel d = k/q*100 //<- Update themometer parameter for the number of marker indices processed Transform AsyncProgress SetPercentComplete d //<- Update progress themometer k = k+1 goto Loop1 else Transform AsyncProgress Hide goto CarryOn endif CarryOn: Warning Show "Completed the copying of all markers...", 1 TryAgain: Save "", r //<--- found all markers, save into a different file if r = 1 Warning Show "Success in writing new file", 2 goto LoopOut endif if r = 2 Prompt "Duplicate filename", "OK" goto TryAgain endif if r = 3 Prompt "Unknown error", "OK" goto LoopOut endif if r = 4 Prompt "File is locked", "OK" goto LoopOut endif if r = 5 Prompt "Not enough permission to save", "OK" goto LoopOut endif LoopOut: CloseGraph ; Change working directory back to original working directory Warning Show "Setting dialog path back to original setting...", 1 Set Folder Path P$ End