1/*
2 * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3 * http://www.gnu.org/licenses/gpl-3.0.html
4 *
5 * $Revision: 7443 $
6 * $Id: edit_startup_script.script 7443 2011-09-01 16:29:16Z mortenmacfly $
7 * $HeadURL: svn://svn.code.sf.net/p/codeblocks/code/branches/release-20.xx/src/scripts/edit_startup_script.script $
8 */
9
10//
11// Sample script that opens the "startup.script" file for editing
12// (check startup.script which registers this script under the menu "Settings->Edit startup script")
13//
14
15
16
17// In order to open the startup.script file, we first have to locate it.
18// Code::Blocks looks for scripts in two places:
19//  1) the per-user scripts dir
20//  2) the global scripts dir
21//
22// One way to achieve this is to use the following lines:
23//
24//  // search for "startup.script" in scripts folders (user and global)
25//  local f = LocateDataFile(_T("startup.script"), sdScriptsUser | sdScriptsGlobal);
26//
27// LocateDataFile() looks in the specified dirs for the file in question.
28// Per-user directories *always* have precedence in this function so if the file
29// exists both in the global dirs as well as in the user directory, the file in
30// the user directory will be returned.
31//
32// In this sample script though, we 'll follow a different procedure. We will manually
33// ask for the user scripts dir and try to open the file there. If it is not opened
34// (which means it doesn't exist), we will copy the script from the global scripts dir
35// and then attempt to open it again.
36// This will make sure that the startup.script to edit is the one the user used to use
37// until the moment this script runs.
38
39
40
41// get user scripts dir
42local scriptsDir = GetFolder(sdScriptsUser);
43
44// try to open it in the editor
45local ed = GetEditorManager().Open(scriptsDir + _T("/startup.script"));
46
47// if it succeeded, we 're done here
48if (!IsNull(ed))
49    return;
50
51// log a message
52LogDebug(_T("First time editing startup.script: copying from global to user scripts dir"));
53
54// nope, we must copy the global script
55local scriptsGlobalDir = GetFolder(sdScriptsGlobal);
56
57// don't try to copy over itself (in case something's wrong)
58if (scriptsGlobalDir == scriptsDir)
59    return;
60IO.CopyFile(scriptsGlobalDir + _T("/startup.script"), // source file
61            scriptsDir + _T("/startup.script"), // destination file
62            false); // don't overwrite (well, we know it's not there but better safe than sorry)
63
64// and try again to open it in the editor
65ed = GetEditorManager().Open(scriptsDir + _T("/startup.script"));
66
67// we don't check anything more because the user's system is too borked to bother :)
68// just display a message if all our efforts have failed
69if (IsNull(ed))
70    ShowError(_T("Could not locate startup.script anywhere on this system..."));
71