How do I add text to a new BBEdit document at creation? - applescript

I open a lot of new documents in Textwrangler/BBedit and I would like them to always have the date printed at the top. I would like this to be automatic so that I don't have to remember to run a script each time.
I'm new to BBEdit but I really like Textwrangler and have used it for years. I read some of the documentation on BB and I think that attaching some Applescript to an event might be the way to go. However, none of the listed events seem quite right, and I don't really want to add dates to existing documents.
I found the following page which was a good starting point:
http://bbeditextras.org/wiki/index.php?title=Scripting_and_Automation
I also found these relevant hooks from the BB docs:
App attachment points
applicationDidFinishLaunching: called when the application has completed
startup.
applicationShouldQuit: called when you choose the Quit (or the application
receives a ‘quit’ event for any other reason).
applicationDidQuit: called when the application has finished shutting down and is about to exit.
applicationDidSwitchIn: called when BBEdit has been brought to the foreground.
applicationWillSwitchOut: called when BBEdit is being put into the background.
Document attachment points
documentDidOpen: called when a document has been opened and is ready for use. (Since BBEdit supports multiple types of documents, your script should allow for the argument to be a document of any type.)
documentShouldClose: called when the application is preparing to close a
document.
documentDidClose: called when the application has closed a document.
documentShouldSave: called when the application is trying to determine whether a given document should be saved.
documentWillSave: called when the application is about to begin saving a
document. (note that this will only be called after a successful return from a
‘documentShouldSave’.
documentDidSave: called after a document has been saved successfully.
documentWillUnlock: called when BBEdit is going to make a document writeable. (For example, when you click the pencil to unlock a document)
documentDidUnlock: called when BBEdit has successfully made a document
writeable.
documentWillLock: called when BBEdit is going to make a document read-only.
documentDidLock: called when BBEdit has successfully made a document readonly.
I don't know if any of those really fit, though. I could also try adding some scripts into the startup folder, but I'm not sure how I would go about say, adding a date to all open documents. I've never done applescript before so it's a little trial and error.
I have this code that I've tried running by itself, and it works fine:
tell application "BBEdit"
tell text window 1
select insertion point after (last character)
set selection to ((current date) as string)
end tell
end tell
I'm just a little lost as to how to get the above code to execute on file creation.

Open Script Editor and paste the following code in a new script document:
use BBEdit : application "BBEdit"
use scripting additions
on documentDidOpen(doc)
set n to the doc's name
set t to the doc's text as string
if n does not start with "untitled text" then return
if t's length > 0 then return
set the contents of the doc to (the (current date) as text) ¬
& linefeed & linefeed
end documentDidOpen
Save it as type script (extension .scpt), and name it Document.documentDidOpen.scpt. Either save it directly, or move it subsequently, to the folder ~/Library/Application Support/BBEdit/Attachment Scripts/; if the folder doesn't exist, create it.
Restarting BBEdit ought not to be necessary, but also couldn't hurt. Now, whenever you create a new document (of any type), it will be headed with the current date and time.

Try using BBEdit's 'Attaching Scripts to Menu Items' feature (p 295 of the v11 User Manual). In a nutshell, if you save a script to the Menu Scripts folder with a name based on the menu/command, then this script will run when that menu item is selected. So in your scenario:
Save the script below to BBEdit's Menu Scripts folder with the filename 'New•Text Document'.
tell application "BBEdit"
set cDate to ((current date) as text)
make new document with properties {contents:cDate}
end tell
As an aside, you can generally avoid using selection with the insertion point, for example:
tell document 1 of application "BBEdit" to set text of ¬
first insertion point of text 1 to ((current date) as text)
In your second 'question' scenario, you could probably cycle through all existing windows with that, for example:
tell application "BBEdit"
set tdCount to count of text documents
repeat with i from 1 to tdCount
set text of first insertion point of text 1 of ¬
text document i to ((current date) as text) & linefeed
end repeat
end tell

Related

Applescript - Looping through System Events is killing my Mac

I have a massive spreadsheet with a titanic number of rows/columns (e.g. ~250 columns, many thousands of rows) that I'm trying to convert into PDFs by looping through each row with AppleScript, copying that row's ~250 variables to TextEdit set to Rich Text (for bold formatting etc), and then using System Events to save the txt as a PDF. Here's a summary of the code:
on run
set initialRow to 1
tell application "System Events"
tell application process "TextEdit"
set frontmost to true
end tell
end tell
repeat
tell application "Microsoft Excel"
-- CLEAR MY ~250 VARIABLES FROM PREVIOUS ROW'S VALUES TO MAKE SURE NOTHING IS CARRIED OVER BY MISTAKE
-- THEN SET MY ~250 VARIABLES TO THE NEXT ROW'S VALUES
if exampleValue is "" then exit repeat
end tell
tell application "TextEdit"
set the text of the front document to ""
-- THEN SET FIRST PARAGRAPH TO MY FIRST VARIABLE PLUS A LINE BREAK SO THEN THERE'S A NEW PARAGRAPH FOR THE NEXT VARIABLE, ETC
-- THEN GO THROUGH ALL OF MY VARIABLES TO IMPORT THE IMPORTANT ONES INTO TEXTEDIT, SET SOME FORMATTING, ETC.
end tell
delay 1
tell application "System Events"
click menu item "Export as PDF…" of menu 1 of menu bar item "File" of menu bar 1 of application process "TextEdit"
delay 1
keystroke exampleValue -- SYSTEM EVENTS TYPES THE NAME OF THE PDF
delay 1
key code 36
delay 1
end tell
set myRow to (myRow + 1)
end repeat
end run
This all runs great, no bugs (seemingly!), no issues at all in small doses. The problem, however, is that something happens as the script runs where it seems to be tying up more and more memory somewhere; everything is fine for the first hundred or so rows, but at some point my Mac stops running anything at all, i.e. whether I let the script run until it starts producing super random errors (I could collect them if helpful, but it's like a random different error each time so not much help there) or even if I let the script run for a while and then stop it before it errors out - it will let me stop the script but then I can't actually quit out of Script Editor or TextEdit or Excel, my keyboard stops working, I can't Force Quit anything, can't Reset the computer, etc. It's just a complete meltdown of the machine unlike anything I've encountered, and the only way to get back to work is to force a hard boot with the power button.
I've never had this problem with my other scripts, but I also don't usually use System Events, so my hunch is that it's something to do with that. Do I need to be 'resetting' System Events somehow, or clearing out the memory for some reason, or...? Thanks for the help!!
Figured it out! After trying the script one more time with Activity Monitor running, I discovered that each time it iterates through, 3 new processes were popping up - Core Sync, Dropbox Finder Extension, and SnailSVNLite - and then never going away! So if I ran through the script 500 times, I'd end up with 1500 new processes running, which was almost certainly what was wrecking me though I have no idea why telling System Events anything was doing that. I looked around online, and it turns out those are all Finder Extensions that had been turned on at some point long ago, so just needed to go to System Preferences > Extensions > Added Extensions and then uncheck those 3 extensions - and then problem solved!!

Get path to original item of a file alias when it's missing

I'm trying to write a script to correct the path to an original item of a file alias. I know what's wrong with the path (I moved a folder), so if I can get what the system thinks the path is, I can fix it.
The problem is that the script simply reports that the original can't be found and not where the original should be.
Is there a way to grab that from with AppleScript? It's there in the Get Info... box, so it's stored in the file. I suspect I could use some bash way to get it with a do shell script, but I'm curious about staying within AS.
Years ago, I also had issue with my alias, when changing folders. This is part of the script I made which looks into Get Info window. Since, may be the Get Info window has been changed on last OS version, therefore, some adjustments are required, but it gives you the direction to go:
on Origine_Alias(F_Alias)
set R to ""
tell application "Finder" to open information window of file (F_Alias as text)
tell application "System Events" to tell process "Finder" to set R to value of static text 18 of scroll area 1 of front window
tell application "Finder" to close front window
return R
end Origine_Alias
The returned string is in Unix format: /folder/my/file

How to select file using AppleScript in Finder prompt?

I am working with Selenium on macOS to automate sending images using WhatsApp web in Google Chrome. The task involves uploading the image, and for that a system(Finder) prompt comes up to select the file. It's done in Windows using AutoIt.
I tried looking up how to automate this task in macOS, and I believe AppleScript can be used for it. Since I have no experience in GUI scripting, any help would be appreciated.
Thanks.
I was able to find the answer on another post on Stack Overflow. I have added the answer for anyone who comes across the same problem.
tell application "System Events"
keystroke "G" using {command down, shift down}
delay 1
keystroke "/path/to/file"
delay 1
keystroke return
delay 1
keystroke return
delay 1
end tell
I don't advocate GUI scripting any more than the burning down of the Amazon, but it seems to be necessary for this task, and I wanted to provide you with an example of a GUI script that tries its best to minimise the unpleasantness of the user experience, and aim for fewer weak points in the code where GUI scripts are most likely to falter.
If you know the path to your file—which I assume you do in these sorts of situations, as your script keystrokes the filepath—then you might find the following technique saves a few steps, and feels a bit more graceful in how it gets executed:
set filepath to "/path/to/image.jpg"
-- Copy file object to clipboard
set the clipboard to filepath as «class furl»
-- Make sure Chrome is in focus and the
-- active tab is a WhatsApp tab
tell application id "com.google.Chrome"
activate
if the URL of the active tab in the front window ¬
does not contain "web.whatsapp.com" then return
end tell
-- Paste the clipboard contents
-- and hit return (send)
tell application id "com.apple.SystemEvents"
tell (process 1 where it is frontmost) to tell ¬
menu bar 1 to tell menu bar item "Edit" to tell ¬
menu 1 to tell menu item "Paste" to set Paste to it
if (click Paste) = Paste then keystroke return
end tell
The if (click Paste) = Paste check should negate the need for a delay, as it explicitly forces AppleScript to evaluate the click command before going on to issue a keystroke. However, I can't test this under all possible conditions, and if there are other factors, like CPU usage, or process freezes, that are likely to give the script a chance to jump ahead, then just insert a small delay after then and move keystroke return down onto its own line.
If you wish to remove the file object from the clipboard afterwards, then simply add as the final line set the clipboard to (and just leave it blank after the word "to", which will clear the clipboard's contents). Of course, this won't affect any clipboard history data you might have if you use a clipboard managing app, only the system clipboard's current item.

AppleScript to download file

I need to write an AppleScript to open Safari and go to https://www.stats.govt.nz/large-datasets/csv-files-for-download/ to download the first csv file
Rather than doing your work for you by providing you with a code snippet to accomplish this task, I will instead provide you with a great resource. Cube MG has a bunch of tutorials that are really helpful for interacting with web page elements via Safari. Check it out here: http://www.cubemg.com/how-to-click-a-button-on-a-web-page-with-applescript/
There are other ways that you could approach something like this such as using the command line but that is a bit more advanced. Check out Cube MG and see what you can learn!
tell application "Safari"
make new document with properties ¬
{URL:"https://www.stats.govt.nz/large-datasets/csv-files-for-download/"}
repeat
if the front document's source contains "What are CSV files?" then ¬
exit repeat
delay 0.5
end repeat
tell the front document to set CSVfiles to ¬
do JavaScript "Array.from(document.links,
x=>x.href)
.filter(x=>x.indexOf('.csv')!=-1);"
end tell
This gets you the links to all the CSV files. To extract the first one, use item 1 of CSVfiles.
NB. Don't forget to enable Allow JavaScript from Apple Events in Developer menu (a hidden menu, made visible through Preferences > Advanced > Show Develop menu in menu bar)

How to pass data from a FileMaker field to an Applescript

I need to delete a file off of my Cincinnati Laser from FileMaker. It gets it there via a FMScript that export field contents of a container to this location. So I know the file name and the path its all built in a calculation field on that record. But I don't know how to get that info into an Applescript using the FM12 "Perform AppleScript" script step
When I hard code the path and file name (Shown Below) it works.
set xpath to "Titanium Brain:Users:smartin:Desktop:Laser:1512-clr-c.cnc"
tell application "Finder"
delete file xpath
end tell
When I try to pass the field contents (Shown Below) it doesn't work.
set xpath to Laser::gCNCPath
tell application "Finder"
delete file xpath
end tell
What am I missing?
The problem with Perform AppleScript with calculations is always the managing of quotes and returns. Putting exactly the following into the 'Calculated Applescript' box of the 'Perform Applescript' script step should work for you:
"set xpath to " & Quote ( Laser::gCNCPath ) & ¶ &
"tell application \"Finder\"¶" &
"delete file xpath¶" &
"end tell"
Honestly, though, the whole thing gets pretty ugly pretty quickly. If you have security locked down appropriately, I'd be more inclined to put the whole script into the Laser::gCNCPath field
set xpath to "Titanium Brain:Users:smartin:Desktop:Laser:1512-clr-c.cnc"
tell application "Finder"
delete file xpath
end tell
And then, for the Perform Applescript, you only need to call the field:
Laser::gCNCPath
Whenever I've needed to pass information from FileMaker to AppleScript (and I'll admit, it's been a while since I've done so), I've used the Native AppleScript field in the Perform AppleScript dialog box and used a global field to store the "parameter" that AppleScript needs and used AppleScript to pull that information.
set xpath to cell "gCNCPath" of layout "Laser" of current file -- double check this syntax, I'm working from memory
tell app "Finder" to delete file xpath

Resources