How to replace in selection using AppleScript for BBEdit? - applescript

Is there a way to replace text within a selection using Applescript for BBEdit? I have a replace script, and I'd like to run it for only selected text. I know you can replace in selection using the find menu, but I can't find anything that says it does this in the AppleScript dictionary for BBEdit. Thanks.

To use an application's scripting dictionary, specific commands aren't necessarily declared, just their syntax and options. Usually you can look at a command to see what the parameters are, which also defines the data types that it is expecting - in this case for example, the replace command has a searching in parameter, which can be anything.
The answer is similar to the one to your other topic, you just need to further define where to search. For example, the selection is a property of a window, and a window is a property of a document:
tell application "BBEdit"
tell front document
replace "12:" using "{#pl}:" searching in its window's selection
end tell
end tell

Related

Automator adding unwanted line breaks

I'm using Automator to create an HTML page and everything works great but I'm running into one small problem. The user is asked for information at the beginning that is then set into variables. The page is created by grabbing some code using Get Specified Text and copying it to the clipboard, getting one of the variables and then putting them both into a text document. This process is then repeated several times, eventually creating an HTML file. I'm running into issues because Automator is creating line breaks (maybe carriage returns?) in between each bit of specified text and each variable. So, what I want to look like this:
<code grabbed using "Get Specified Text" followed by a Variable. And now some more text and another Variable.>
ends up looking like this:
<code grabbed using "Get Specified Text" followed by a
Variable
. And now some more text and another
Variable
.>
This is breaking my page in a few parts. Is there a way to prevent these line breaks?
The items passed along from action to action are in a list, so it appears that setting the TextEdit contents separates the individual items by a newline, which is the normal paragraph delimiter.
Many of the text actions assume TextEdit and/or rich text and don’t use variables (or get along with other plain text actions), so a Run AppleScript action can be used before an action to convert or concatenate items, for example (Mojave):
Automator (or TextEdit for that matter) isn’t really a very good tool for HTML editing. You might take a look at BBEdit (the light version is free), which also has excellent AppleScript support.
EDIT:
Use the following in a Run AppleScript action to combine the text using a specified delimiter (this example uses an empty string):
on run {input, parameters}
set separator to "" -- text to separate the items with
set tempTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to separator
set output to input as text
set AppleScript's text item delimiters to tempTID
return output
end run

Xcode AppleScript : change label which content of variable

Here is my script :
property MyLabel : missing value
on buttonClicked_(sender)
set the plistfile_path to "~/Desktop/MY_DATA.plist"
tell application "System Events"
set p_list to property list file (plistfile_path)
-- read the plist data
set theMyDataFromPlist to value of property list item "DATA1" of p_list
end tell
end buttonClicked_
This gonna take the data I want from a plist and set in a variable (theMyDataFromPlist).
How can I print this variable on the label "MyLabel" and make the script auto refresh when the data change ?
also, when I click on the text (or another button), can I copy the value to the clipboard. (would set the clipboard to theMyDataFromPlist work?)
I also wonder is that possible to do the same with Swift?
How can I print this variable on the label "MyLabel"
Where would you like to print the variable? You can make AppleScript display a dialog:
set variable to 42
display dialog "The value of variable is " & variable
AppleScript is not designed as a terminal language, it is for UI scripting, so it's not like most scripting languages that just offer a print as where would the user see the printed value.
and make the script auto refresh when the data change ?
Not quite sure what you expect here. That your system magically runs your script whenever the content of a file changes? That's not going to happen. You can create a launchd job and have launchd monitor that file and then execute your script when the file changes; this is described here:
https://discussions.apple.com/message/13182902#13182902
But some process will have to monitor the file and if your script should do so, it has to be running all the time, non-stop. Then you could make some code run once every X seconds, checking the file last modification date and whenever that changes, re-read the plist. This polling is super ugly but the best thing that AS can do out of the box.
BTW where is the rest? You say
also, when I click on the text (or another button),
can I copy the value to the clipboard.
Which text? Which button? Sound like you have a whole application there but all you showed us are 11 lines script code. You didn't even mention that you have a whole application with a UI. Your question starts with "Here is my script", so you make it sound like this 11 lines is all that you have.
(would set the clipboard to theMyDataFromPlist work?)
Why don't you simply try it out? Pasting that line into ScriptEditor would have taken equally long than asking this question. I just tried it and it turns out that you can only set strings.
This code won't work:
-- bad code
set variable to 42
set the clipboard to variable
But this code does work:
-- good code
set variable to 42
set the clipboard to "" & variable
I also wonder is that possible to do the same with Swift?
Personally I would not even consider writing an application in AppleScript; I'd rather stop writing code before I do that. Of course this can be done in Swift or in Obj-C. Everything you can do in AS can be done in these other two languages and no, the opposite doesn't hold true.
Using Obj-C or Swift, you can also use GCD and with GCD monitoring a file for changes is easy. Just see
https://stackoverflow.com/a/11447826/15809

Support "styled text" in a scriptable Mac application (Cocoa Scripting)

My app supports being scripted with Applescript.
I am trying to make styled text content, stored in NSAttributedString objects, available to an Applescript user.
I thought I could simply deliver styled text with the NSAttributedString class, just like I deliver plain text with the NSString class, but that does not work - Cocoa Scripting then reports that it cannot convert or coerce the data.
I wonder if I'm missing something or if this is just plain impossible with the standard classes supported by Cocoa Scripting?
AppleScript does know the "styled text" type, as seen in this example:
set stxt to "foo" as styled text
So, if AppleScript knows this type by default, shouldn't the Cocoa Scripting engine support it as well somehow?
As always there are many choices for solving an AS problem.
In my scriptable text editor (Ted), I implemented the Text Suite, which is based on rich text (NSTextStorage, a subclass of NSMutableAttributedString). I wanted to be able to script tabs in my paragraphs, so I added a style record, which contains all the paragraph style information. This lets me write scripts like this:
tell application "Ted"
set doc1 to make new document at beginning with properties {name:"Document One"}
tell doc1
set p1 to make new paragraph at end with data "Paragraph One" with properties {size:24, color:maraschino}
set p2 to make new paragraph at end with data "Paragraph Two" with properties {style:style of paragraph 1}
set color of paragraph 1 to blue
end tell
set doc2 to make new document at beginning with properties {name:"Document Two"}
copy p1 to beginning of doc2
properties of paragraph 1 of doc2
end tell
Since p1 is rich text, the second document ends up with both the text and formatting of the first paragraph of the first document.
You can also ask for the properties of a piece of text, where I have implemented the usual Text Suite properties, as well as a "style" property for paragraph style (backed by NSParagraphStyle, since I wanted to be able to script the tab stops):
properties of paragraph 1 of doc2
Result:
{height:60.0, italic:false, size:24, style:{paragraph spacing after:0.0, head indent:0.0, line break mode:0, alignment:4, line spacing:0.0, minimum line height:0.0, first line head indent:0.0, paragraph spacing before:0.0, tabs:{"28L", "56L", "84L", "112L", "140L", "168L", "196L", "224L", "252L", "280L", "308L", "336L"}, tail indent:0.0, maximum line height:0.0, line height multiple:0.0, default tab interval:0.0}, color:blue, width:164.109375, font:"Helvetica", bold:false, class:attribute run}
This works well for passing rich text within my application, but may not be as useful for passing styled text to other applications, which may be what you wanted to do. I think adding a "style" property (of type record) is probably the best way to convey style info for use in other scriptable apps. Then in the second app, the scripter can make use of any properties in the style record that the second app understands.
It looks like there is no implicit support for styled text in AppleScript. And there is also no common interchange record type for passing styled text.
AppleScript was developed in the pre-OSX days when styled text was often represented by a combination of a plain text (in System or MacRoman encoding) and a styl resource. With Unicode came an alternative format of a ustl style format. These are still used with the Carbon Pasteboard API (PasteboardCreate etc.) today. Yet, none of these seem to have made it into the use with AppleScript.
The fact that AppleScript knows of a styled text type has no special meaning. Even its class is just text.
Update
I just found that Matt Neuburg's book "AppleScript The Definitive Guide" mentions styled text and gives an example where it's indeed showing a record containing both the plain text (class ktxt) and style data (class ksty) with data of type styl, just as I had expected above. He also points out that most applications don't use that format, though.
So, it appears using a record with style resource data is indeed the intended way, only that hardly anyone knows about it. Go figure.

Sublime Text disable goto animation

When using the goto function to bring my cursor to a particular line number, say 3017, how do I prevent Sublime from jumping around from line to line until I hit enter?
For instance, in that case, I would jump to the following lines:
3
30
301
3017 (finally)
Sublime Text 3 seems to have two "Goto Line" features:
one which is built in via the "Goto Anything" overlay. Internally, the command to execute this is show_overlay with the arguments {"overlay": "goto", "text": ":"}. This is the default, available from the Goto menu -> Goto Line, and with keybinding Ctrl+G. MattDMo is correct in his answer that it is not possible to disable fuzzy matching in this overlay.
one which is included in the "Default" package as a plugin and shows a small prompt panel at the bottom of the screen. Internally, the command to execute this is prompt_goto_line, with no arguments necessary. This implementation has the behavior you desire, and will only go to the specified line when you hit the enter key. It has no default way of accessing it, but read on... :)
The reason I have mentioned the internal commands above is because Sublime Text makes it possible to add or override keybindings, and also to change or add menu items, and the commands are used to instruct Sublime Text what action to perform.
Therefore, this means that you can choose to override the existing menu item and/or keybinding, (and/or you can create a new menu item and/or keybinding) to use the prompt_goto_line command. The two links I have just provided should give enough detail on how to perform these tasks, but if you would like more specific information, please let me know in a comment and I will provide it.
This feature is by design, and cannot be disabled. Most popup menus in Sublime feature "fuzzy matching", meaning you do not need to type the full search term, just a few letters (for example, pci finds Package Control: Install Package in the Command Palette). The menus also feature instant searching, which is what you are seeing. This means that you do not need to hit Enter to search, just start typing and the matches appear as you type.

BBEdit AppleScript for reformatting multiple files

I'm looking to write an Applescript which will utilize BBEdit — loop through a ton of HTML files and autoformat them (so the indentation is easier to read).
So far I have:
tell application "BBEdit"
activate
open {file "Macintosh HD:TEST DIRECTORY:testfile copy 2.html"} with LF translation
(format mode hierarchical)
beep
display alert "Finished!"
end tell
This applies the transformation to a single file, but has anyone got any suggestions how to apply this to an unknown number of HTML files?
You've almost got it; the trick is that you want to loop through the files returned by open. Thus, you need something like this:
tell application "BBEdit"
set docs to open LIST_OF_FILES with LF translation
repeat with doc in docs
-- format doc
save doc
end repeat
beep -- Or even `say "Finished!" without waiting until completion`
-- if you want your computer to talk to you
display alert "Finished!"
end tell
As you can see, all you need to do is place your formatting code inside this loop (and don't forget to save the files!); the loop will set doc to each element of the list docs in turn, and run the body with that element. If you're not sure how to select the files, one way is choose file with multiple selections allowed; this will pop up a dialog box which will allow you to select as many files as you want. To use it, just replace LIST_OF_FILES with (choose file with multiple selections allowed).
BBEdit will perform a find/replace on any group of files you want. Just hit command+shift+f to bring up Multi-File Search instead of the basic find/replace window.
If you have more than one set of find/replace commands that you need to execute at the same time, you need a Text Factory. See here for details on how to set one up: http://www.barebones.com/products/bbedit/benefitsexercise.html
Does that help?
You should use the Text Factory feature for this. Under "File > New > Text Factory". Any operation you can perform on a single file can be done on any number of files and you can save the operation for future use.

Resources