Sunday, May 28, 2017

#1 2016-10-13 01:47:47 pm

Riku
Member
Registered: 2016-05-23
Posts: 20

Toggle enable/disable a part of script in Script Editor

When writing long scripts with lots of possible if scenarios, in order to keep track of where things go wrong when not running the script in Script Editor, I've learned to set display notification traps after each step to let me know how far the script manages to run. I realized that I was spending considerable amounts of time scrolling through my script, looking for the lines to manually disable and enable to and fro.
While only a real person can determine which parts to enable or disable, I thought that I needed a keyboard shortcut that would toggle the availability of a selected zone of the script, making things much faster.
It's not perfect but it works for me now. See documentation in the beginning of the script.

Applescript:

-----This script toggles a specified part of script on or off, using (*comment out*) method.
----I wanted to have the script emulate keystrokes cmd+c, cmd+x and cmd+v but for some reason I can't get it to work. I'll save the troubleshooting for later and probabyl update this script later but for now I compensated for the script's flaws by the functions of the keyboard shortcut mapper that is anyway mandatory for this script. I've left out the grey parts where the keyboard shortcut actions should've been. If you know how to make them work, feel free to edit the script as you like. Below are the two alternative methods for putting this script to work:

-----#### MANUAL VERSION
-----1. Use the cursor to select an area of text in Script Editor window.
-----2. Press Cmd+X
-----3. Press the keyboard shortcut that runs this script file
----4 Press Cmd+V

-----#### ALL-INCLUSIVE VERSION (I used BetterTouchTool for mapping)
-----1. Use the cursor to select an area of text in Script Editor window.
-----2. Press the keyboard shortcut that's been mapped to launch a chain of subsequential actions:
-------a) Trigger keyboard shortcut Cmd+X
-------b) Trigger this script
-------c) Trigger keyboard shortcut Cmd+V
-------d) OPTIONAL: Trigger keyboard shortcut Cmd+K (Compiles script)

set Starter to "(*"
set Ender to "*)"
(*tell application "System Events"
       keystroke "c" using command down
       delay 0.5
end tell*)

delay 0.1
set SelectedText to the clipboard as text
if (SelectedText contains Starter) is true and (SelectedText contains Ender) is true then
   set IsDisabled to true
else if (SelectedText does not contain Starter) is true and (SelectedText does not contain Ender) is true then
   set IsDisabled to false
else
   display dialog "Make sure that your selection is precise." with title "Failed to specify text, phase 1."
   error -128
end if
if IsDisabled is true then
   set ScriptArea to the number of characters of SelectedText
   set StrippedArea to (ScriptArea - 2)
   set EnabledScript to (text 3 thru StrippedArea of SelectedText) as string
   set the clipboard to EnabledScript as string
   (*tell application "System Events"
       keystroke "x" using command down
       delay 1
       keystroke "v" using command down
   end tell*)

else if IsDisabled is false then
   set CommentOut to ("(*" & SelectedText & "*)") as text
   set the clipboard to CommentOut as text
   (*tell application "System Events"
       keystroke "x" using command down
       delay 1
       keystroke "v" using command down
   end tell*)

else
   display dialog "Make sure that your selection is precise." with title "Failed to specify text, phase 2."
   error -128
end if
delay 0.1

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.037 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)