In AppleScript and BBEdit how can you check links? - html

In BBEdit there is the command under Markup -> Check -> Document Links shortcut cmd+control+k that checks all links. When I look at the dictionary under BBEdit > HTML Scripting -> check links it shows:
but when I try to script against a project with:
set theResult to check links of active document of project window 1
I get an Error of item, when I try to check based on the filename with:
set foobar to (name of active document of project window 1) as string
set theResult to check links of foobar
I still get the same error, if I try:
set projectPath to file of project document 1
set theResult to check links of projectPath
I get a returned of {}. Thinking it was an issue with not adding with show results I changed it to:
set theResult to check links of projectPath with show results
but I get a return of activate
When I search through Google I'm unable to find a solution on if it's possible to return a boolean on wether the links in the HTML file are valid when scripting through the content. So my question is, how can I get AppleScript to tell me the links are valid in BBEdit with check links?

To check the links from the file of the active document:
tell application "BBEdit"
set theFilePathOfFrontProject to file of text document 1 -- get the path of the selected file in the front project window
set theResult to (check links of theFilePathOfFrontProject) is {}
if theResult then
display dialog "All links appear to be valid"
else
display dialog "Some links appear to be not valid"
end if
end tell
Informations :
set projectPath to file of project document 1, this command return the path of the project (check links on this file will always return an empty list), the path will be file "fullpath:someName.bbprojectd", it's not the path of the selected HTML file in the project.
To get path of all files of the project : set allFilePaths to project collections of project document 1 -- list of paths

I believe this worked last time I used it, I'm on mobile about to board a flight so syntax may have gotten mumbled.
set theFile to ((path to documents folder) as string) & "test.html"
set theResult to check links of file theFile
To use system events to press keys, you could use a separate tell block, or create a handler like so.
on checkLinks()
tell application "System Events"
keystroke "k" using {command down, control down}
end tell
end checkLinks
then call the handler as usual
my checkLinks()

Related

Saving a page and clicking on the save popup in chrome using AppleScript

I want to download a txt file, opened in a tab in chrome using AppleScript. I want the save as dialog of Mac to provide the default extension and the name of the file.
tell application "Google Chrome" to tell active tab of window 1 to save as "r1.txt"
I tried this approach, and some other approaches like
activate application "Google Chrome"
tell application "System Events"
tell process "chrome"
keystroke "s" using {command down}
delay 1
click button "Save" of sheet 1 of window 1
end tell
end tell
still am not able to click the save button in the modal.
This works for me using the latest version of Google Chrome and the latest version of MacOS Mojave
activate application "Google Chrome"
tell application "System Events"
repeat while not (exists of menu bar item "File" of menu bar 1 of application process "Chrome")
delay 0.1
end repeat
click menu bar item "File" of menu bar 1 of application process "Chrome"
repeat while not (exists of menu item 11 of menu 1 of menu bar item "File" of menu bar 1 of application process "Chrome")
delay 0.1
end repeat
click menu item 11 of menu 1 of menu bar item "File" of menu bar 1 of application process "Chrome"
repeat while not (exists of UI element "Save" of sheet 1 of window 1 of application process "Chrome")
delay 0.1
end repeat
click UI element "Save" of sheet 1 of window 1 of application process "Chrome"
end tell
My approach to this problem was to try and avoid scripting the UI if possible, which can be problematic and unreliable. Instead, I decided to use the shell command curl to do the downloading job for us instead of trying to manipulate Chrome into doing it.
All we need is the location of where to save the file to, which I've set as the location to which Google Chrome defaults to, namely ~/Downloads.
property path : "~/Downloads" -- Where to download the file to
use Chrome : application "Google Chrome"
property sys : application "System Events"
property window : a reference to window 1 of Chrome
property tab : a reference to active tab of my window
property URL : a reference to URL of my tab
property text item delimiters : {space, "/"}
on run
-- Stop the script if there's no URL to grab
if not (my URL exists) then return false
-- Path to where the file will be saved
set HFSPath to the path of sys's item (my path)
-- Dereferencing the URL
set www to my URL as text
-- Extract the filename portion of the URL
set filename to the last text item of www
-- The shell script to grab the contents of a URL
set sh to the contents of {¬
"cd", quoted form of the POSIX path of HFSPath, ";", ¬
"curl --remote-name", ¬
"--url", quoted form of www} as text
## 1. Download the file
try
using terms from scripting additions
do shell script sh
end using terms from
on error E
return E
end try
## 2. Reveal the downloaded file in Finder
tell application "Finder"
tell the file named filename in the ¬
folder named HFSPath to if ¬
it exists then reveal it
activate
end tell
end run
It's a longer script than your present one, but most of it is declarations of variables (and properties), after which the script does two simple things:
Grabs the URL of the active tab in Chrome, and downloads the contents of that URL into the specified folder, retaining the same filename and extension as the remote file;
Once the download is complete, it reveals the file in Finder.

Applescript - open a new tab

My AppleScript abilities are rather limited, so please forgive what may be a simple question.
I have this script as an Automator Service which will open a series of aliases in new windows.
Triggered by key command in Finder via prefs>keyboard>shortcuts>services.
Service receives selected files or folders in Finder
on run {input, parameters}
repeat with aFile in input
tell application "Finder"
try
set origFile to original item of aFile
set aWindow to make new Finder window
set aWindow's target to origFile's parent
select origFile
end try
end tell
end repeat
end run
I'd like to try open in tabs instead, preferably without resorting to GUI scripting.
set aWindow to make new Finder window appears to have no equivalent set aWindow to make new Finder tab & scouring Apple's online documentation for 'make' or 'tab' has proven pretty fruitless... or rather much fruit, all of the wrong variety :/
I have a GUI version from another source
on new_tab()
tell application "System Events" to tell application process "Finder"
set frontmost to true
tell front menu bar to tell menu "File" to tell menu item "New Tab"
perform action "AXPress"
end tell
end tell
end new_tab
so, failing the direct approach, how could I fold this into my existing script?
MacOS 10.13.4
With the macOS defaults for both the Open folder in tabs instead of new windows preference in Finder unchecked, and the Dock preference Prefer tabs when opening documents: in System Preferences set to In Full Screen Only, then the following example AppleScript code should work as wanted with incorporating your original AppleScript code and the code of the new_tab handler.
on run {input, parameters}
set madeNewWindow to false
repeat with i from 1 to count input
tell application "Finder"
if (kind of item i of input) is equal to "Alias" then
set origFile to original item of item i of input
if not madeNewWindow then
set theWindow to make new Finder window
set madeNewWindow to true
else
my makeNewTab()
end if
set theWindow's target to origFile's parent
select origFile
end if
end tell
end repeat
end run
on makeNewTab()
tell application "System Events" to tell application process "Finder"
set frontmost to true
tell front menu bar to tell menu "File" to tell menu item "New Tab"
perform action "AXPress"
end tell
end tell
end makeNewTab
On my system it was not necessary for me to use the delay command however, delay commands may or may not be needed on your system and if so, add as necessary while adjusting the value as appropriate.
Coded for use in a Run AppleScript action in an Automator service where Service receives selected [files or folders] in [Finder].
Requires Finder to be added to Accessibility under Security & Privacy in System Preferences.
Tested under macOS High Sierra.
Note: The example AppleScript code is just that and does not employ any other error handling then what's shown and is meant only to show one of many ways to accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.
AppleScript to open Tabs in Finder from a list of paths in POSIX
Running this in the application Script Editor opens a predefined list of tabs from a list that one can set in normal POSIX file path, like /path/to/folder-or-file. To get a link to your folder or file, either press CMD+i or press File->Get Info in the menu (or just right-click on the file/folder itself). In the small window popping up, copy the path from the field named General -> Where: or get it from the terminal using print working directory (pwd) command and copy-pasting into the item-vars in the script below. Using a repeat loop we go through the vars included in the list and pull up one tab for each item. Very handy for those projects that use the same folders but lots of them!
on convertPathToAlias(thePath)
tell application "System Events"
try
return (path of disk item (thePath as string)) as alias
on error
return (path of disk item (path of thePath) as string) as alias
end try
end tell
end convertPathToAlias
set item1 to "/Users/username/Desktop/myfolder1"
set item2 to "/Users/username/Desktop/myfolder2"
set item3 to "/Users/username/Desktop/myfolder3"
set item4 to "/Users/username/Desktop/myfolder4"
set myList to {item1, item2, item3, item4}
set default_path to convertPathToAlias(item1)
tell application "Finder"
activate
open default_path
end tell
repeat with theItem in myList
set current_path to convertPathToAlias(theItem)
tell application "System Events" to keystroke "t" using command down
delay 0.3
tell application "Finder" to set target of front window to current_path
end repeat
using the initial function convertPathToAlias() we convert a normal path into the Applescript alias format. Read more about Applescript folders and files actions here.
I also added the delay command as the finder sometimes has an issue with loops and pulling up new tabs. At least on my machine.
This is based off a script with static links to folders.
To make this script import an external TXT file, check out this post on how to turn a text file into a list and run this from that instead!

Sublime text HTMLPrettify - disable formatting *.min.* files

I am using HTMLPrettify with formatting set to "on save". Everytime I open and change the contents of a minified file, the package simply expands it and formats it the way it has to be, but that is not what I want. I want to exclude all files that have .min. in their extensions, so they can remain minified on save.
How can I do this?
SOLUTION: As MattDMo explained in his solution, there is no setting comming out of the box for this HTMLPrettify package.
There is no setting to do this. However, if you feel comfortable editing the plugin's code, you can do the following. Select Preferences → Browse Packages… to open your Packages folder in your operating system's file manager. Navigate to the HTMLPrettify folder and open HTMLPrettify.py in Sublime.
Go to line 22, which should be a comment on the very first line of the run method in the HtmlprettifyCommand class. Put your cursor just before the # symbol and hit Enter a few times to insert some blank lines. Then, go back to the very beginning of first blank line (not the indented beginning, the very beginning of the line) and insert the following code (the indentation should already be correct):
from os.path import split
try:
if ".min." in split(self.view.file_name())[1]:
return
except TypeError:
pass
Save the file, and the plugin should reload automatically. You can always restart Sublime to make sure. To explain the code: first we import os.path.split(), which separates the filename from the rest of the path. Next, we try to see if the string .min. is in the filename (os.path.split() returns a 2-part tuple containing the full path at the 0 index, and the filename at the 1 index). If it is, we return the method, ensuring that it does nothing else. If the string is not found, the code just continues on like normal. A TypeError exception may be raised by split() if self.view.file_name() doesn't contain anything, which would be the case if you're working in an unnamed buffer. If the TypeError does occur, we catch it and pass, as it means there is no .min. in the filename.
Warning
With this change, the plugin will no longer work on any minified file with .min. in the filename, even if you want to un-minify it. You'll either have to copy the contents to a blank buffer or rename the file first.
Good luck!

Why does my file download link seem to work, but is unable to find the file?

I added this HTML to a page that I render via a REST call:
StringBuilder builder = new StringBuilder();
. . .
builder.Append("<p></p>");
builder.Append("<a href=\"/App_Data/MinimalSpreadsheetLight.xlsx\" download>");
builder.Append("<p></p>");
. . .
return builder.ToString();
My ASP.NET Web API project has a folder named "App_Data" which does contain a file named "MinimalSpreadsheetLight.xlsx"
The download link is indeed rendered on the page, and clicking it does appear, at first, to download the file (it has the Excel icon, and it bears the file name), but beneath that it says "Failed - No file":
Is the problem with my HTML, or the path I'm using, or file permissions, or what?
I've only tested this with Chrome, so far, BTW. IOW, it's not an IE issue.
UPDATE
I tried it with a leading squiggly, too:
builder.Append("<a href=\"~/App_Data/MinimalSpreadsheetLight.xlsx\" download=\"Spreadsheet file\">");
...yet, alas, to no avail.
UPDATE 2
I changed the pertinent line of HTML to this:
builder.Append("<a href=\"App_Data/MinimalSpreadsheetLight.xlsx\" download=\"Minimal Spreadsheet file\">");
...and it displays in the source like so (with some context):
<p>(Invoice Count excludes credits and re-delivery invoices)</p><p></p><p></p><a href="App_Data/MinimalSpreadsheetLight.xlsx" download="Minimal Spreadsheet file">
...but the link does not appear at all.
UPDATE 3
I was misled by this reference, which showed no text being added; I changed the code to this:
builder.Append("Spreadsheet file");
...(adding "Spreadsheet file" and closing out the anchor tag), and now the link appears; however, I still get the "Failed - No file" msg, and 2-clicking the "downloaded file" does nothing.
UPDATE 4
I tried two other permutations of what's seen in Update 3, namely with the forward whack reintroduced prior to "App_Data":
builder.Append("Spreadsheet file");
...and with both the squiggly prepended and the forward whack:
builder.Append("Spreadsheet file");
...but the results are the same in any of these permutations ("Failed - no file").
UPDATE 5
I also tried it without the "App_Data" at all, on the off change that is not needed:
builder.Append("Spreadsheet file");
...but the same "Failed - No file" is the result of that attempt, too.
UPDATE 6
Okay, so I tried this, too (single quotes):
builder.Append("<a href='/App_Data/MinimalSpreadsheetLight.xlsx' download='Minimal Spreadsheet file'>Spreadsheet file</a>");
...but no change. The file is there:
...so why is it not seen or accessible?
UPDATE 7
This:
string fullPath = HttpContext.Server.MapPath("~/App_Data/MinimalSpreadsheetLight.xlsx");
... (which I got from here) fails to compile with, "An object reference is required for the non-static field, method, or property 'System.Web.HttpContext.Server.get'
2-clicking the err msg highlights just "Server"
UPDATE 8
This (which I got from the same place as what I tried in Update 7):
string justDataDir = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
string url2 = string.Format("Spreadsheet file</button>", justDataDir);
builder.Append(url2);
...does nothing; clicking the link doesn't even give me a fake/failed download now...
justDataDir is:
C:\Projects\ProActWebReports\ProActWebReports\App_Data
url2 is:
Spreadsheet file</button>
UPDATE 9
I noticed on further fine-tooth-combing that url2 had a forward whack in it; I changed it so that all the whacks were back, but it made no difference to Update 8's results: clicking the link does nothing whatsoever.
If somebody solves this, it will definitely get bountified after the fact.
UPDATE 10
Maybe what I really need to do is, instead of the simple html, add some jQuery that will download the file. But the question is, can jQuery access the App_Data folder any better than raw/simple html can?
The app_data folder is used by iis and asp.net as a private area in which to put database files which can only be accessed by code running on the server.
If you try to access the folder directly via your browser you will get a permissions error.
In order to make the files available for download, move them the a folder under 'Content' (if you have an mvc site) and ensure that your web.config allows the .xlsx exention to be downloaded.
It may depend on what version of iis you are using.
Downloading Docx from IE - Setting MIME Types in IIS

Server Side Includes for HTML

How do I enable Server Side Includes for html file hosted on IIS 8.5? Like:
<!--#include virtual="filename.htm"-->
Currently, I don't see include file (html) content when I open page in browser.
Edit: I manually added module mapping of ServerSideIncludeModule for website & still doesn't work.
You have the correct module mapping handler.
When you add the module mapping under Handler Mapping in IIS 8.5 double check that you specify *.html as the file type; don't forget the asterisk (the *). I had added it only as .html, and that won't work.
Also, when you add the module mapping, make sure you click on the "Request Restrictions..." button; on the Mapping tab for restrictions, the checkbox for "Invoke handler only if request is mapped to:" should be checked, and the selection should be set to File. More importantly, on the Verbs tab for the restriction, make sure that the lower dot is selected for "One of the following verbs:" and in the field below you should have "GET, HEAD, POST". Lastly, on the Access tab you should have "Script" selected.
Lastly, if you haven't done the appcmd.exe to set ssiDisable to false, you'll need to run this at at command prompt (run the command prompt as Admin).
cd %windir%\system32\inetsrv
appcmd.exe set config "Name_of_website_as_it_appears_in_IIS_Manager" -section:system.webServer/serverSideInclude /ssiExecDisable:"False" /commit:apphost