Applescript - open a new tab - tabs

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!

Related

Invoking series of commands in a new tab in cmder

TLDR:
I want to use something like -new_console:t:tabname to open a new named tab in cmder and then transfer focus/control to that tab so that the rest of the commands I'm sending from a Python script run in that console instead. Or, I want to rename a cmder tab from a script running in the console.
DETAILS:
I often have to execute a series of commands in cmder in order to test the latest code from our continuous integration environment. Because there are several applications I often have to have running at a time, it would be helpful for me to have the cmder tabs named according to which application they are running.
The only way I've found to set a tab name from within cmder (apart from manually with mouse clicks) is to do so with the -new_console:t:tab_name command. But that only runs the next command in the newly opened tab, and not all the things that come after it.
I kick off all my commands with a Python script that accepts parameters to let me control which application opens and how things behave. I'd like to do something like this:
os.system('pwd "-new-console:t:' + args.app + '"')
so that a new tab opens with the name of the app I'm about to invoke in it, starting with an indication of the present working directory. But then I'd like all the commands that follow from the Python script to be run in this new tab instead of in the tab used to kick off the Python script. This includes printing some flowerboxed comments, but also invoking a local application server that will continue running.
Is there any way to, as you create a new (named) tab in cmder, transfer focus to that tab so that all future commands run in that tab instead of the initiating tab? Alternately, is there any way from within a cmder console to rename the cmder tab it's running in? That would be just as good.
Thanks!
Thanks for looking, but I found the answer.
In the bottom right corner is a hamburger stack. Click on that and select Settings. In General --> Tab bar, change the Console setting from the default %n to %s. Then the "title" command will change the tab name.

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.

In AppleScript and BBEdit how can you check links?

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()

How to start Sublime Text with selected project using a local .sublime-project file?

I may be missing the obvious, but can't seem to solve this fairly simple & typical case (with v3 build 3022 on Debian or XP, in case it matters):
Start Sublime Text opening a project "myprj", specified on its command-line,
using default.sublime-project located under that project's dir (say "/repo/myprj"),
automatically re-opening the last open files of that project (i.e. its workspace),
using default.sublime-workspace for that (also from the same project dir)
but (obviously) not auto-opening any other files remembered from non-project sessions,
and automatically saving all open files (to the above default.sublime-workspace) on exit.
Ideally:
$ sublime_text --project /repo/myprj/default.sublime-project
should just work. But it doesn't (see below).
Another approximation that seemed reasonable:
setting "hot_exit" and "remember_open_files" to false, and then invoking ST with:
$ sublime_text --data /repo/myprj --project default.sublime-project
But ST3 either doesn't find the project file (via --data), unless I chdir there first (--data seems to be no longer supported?), and it either doesn't auto-open any workspace files from last time (despite a previous "Project / Save Workspace As..."), or, if I set "remember_open_files" back to true, it just re-opens the last open files regardless of the project given on the command line.
I guess the issue is the workspace file not being handled automatically for some reason, and I'm just missing some trivial step somewhere. (The official docs (and also the unofficial) seem to discuss command-line switches for OS X only, and asking for --help didn't actually help with this one.)
(Please note: I wouldn't like to launch ST first, and then switch to some project from inside manually, and I also don't want to store the sublime-project/-workspace files outside of the prj. dir.) Thanks a lot!
I seached for a solution for a similar problem these days and didn't find a proper way. So i created an automator app with a small apple script. Maybe this helps you too.
Open Automator and Choose news Application.
Create an action to start Sublime Text 2
Insert an action to perfom the following apple script:
delay 0.2
tell application "System Events"
tell process "Sublime Text 2"
tell menu bar 1
tell menu bar item "Project"
tell menu "Project"
tell menu item "Recent Projects"
tell menu "Recent Projects"
click menu item "~/yourproject.sublime-project"
keystroke "p" using {command down, shift down}
end tell
end tell
end tell
end tell
end tell
end tell
end tell
For anyone still looking to do this, you can launch the built-in command line tool and launch it with a project like so (Mac OSX):
"/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" --project "absolute-path-to-your.sublime-project"

Getting at VBA/Access Source Code

I'm rewriting a legacy access / VBA application in C#. While I am limited as a VBA programmer I am under the impression that the code is minimally compiled and runs almost as a script ? Obviously no security / VBA you can just hit alt + f11 to get at the source code is there a good way to decompile / get at this code?
So I tried this: http://forums.databasejournal.com/showthread.php?t=34222
which appears to be about how to decompile .mdb files.
However the program quickly recompiled itself - or at least says it is recompiling itself in the lower left status bar. Any ideas?
Here are some suggestions (some of which I'm repeating from comments I've made above):
Alt-F11 is not my usual method of opening the VBE, because I usually want to go to the Immediate Windows. Try Ctrl-G, instead.
If both Alt-F11 and Ctrl-G fail to open the VBE, then perhaps the AllowBypassKey property of the database has been changed to False. To get code to change this, search the Access help file for AllowBypassKey (in the VBE, from the help menu, search for "AllowBypassKey"). However, you won't be able to run the code within the database you're trying to investigate if AllowBypassKey is turned OFF, so you can run this code:
//
On Error GoTo Change_Err
Dim db As DAO.Database
Dim prp As Variant
Const conPropNotFoundError = 3270
Set db = DBEngine.OpenDatabase("C:\Databases\MyDatabase.mdb")
db.Properties("AllowBypassProperty") = True
exitRoutine:
If Not (db Is Nothing) Then
db.Close
Set db = Nothing
End If
Exit Sub
errHandler:
If Err = conPropNotFoundError Then ' Property not found.
' do nothing and exit
Resume exitRoutine
End If
Then you should be able to open the database when holding down the SHIFT key (which bypasses any defined startup routines, which might have been shutting off access to the VBE).
If the file is an MDE, there is no source code. You can find out if it's an MDE by checking this property:
?CurrentDB.Properties("MDE")
If it's an MDE (the file can have any extension), this will return "T". If it's not an MDE, it will throw an error (because the property doesn't exist).
Other things to check might be how many modules there are. If you have the database open and can get to the Immediate Windows (Ctrl-G), then this will tell you if there are any modules:
//
?CurrentProject.AllModules.Count
You also might be able to see what's in the database by opening up the Object Browser in the VBE (F2) and selecting the project name in the dropdown at the top (it will say "" by default
Last of all, you may think that it could be protected by Jet ULS, but starting with Access 2000, that's not a big possible, as there's nothing but a password on the VBA project available (i.e., it's no longer covered under Jet ULS). I would expect that if it were password-protected, you'd be prompted for the password somewhere along the line, so you'd already know that.
If it's an .mde you are out of luck. You need to get hold of the .mdb containing the source VBA code that was compiled to create the .mde
HOW TO VIEW SOURCE CODE (.mdb FILE)
Select the file
Hold the 'shift' key
Then double click the file without releasing the 'shift' key
Navigate to the Program Folders on your computer.
Open the MS Office Folder
Navigate to MSAccess.exe and make a shortcut of it.
Move the shortcut to where your database resides (makes life easier and you'll need to do this for each database to decompile).
(optional) Rename the shortcut to something like "decompiler for my database whose name is...." so you know that opening it blows away your compiled code
Right-click on the shortcut to view its properties. In the Target field of the properties dialog box for the shortcut you'll see something like "C:\Program Files\Microsoft Office\Office\MSACCESS.EXE"
In the target field, right-arrow to the end of the pathname, type a space, and then, in quotes, type (or paste) the full pathname of the location of your MS Access file to decompile (easiest way is to copy from the address bar on your window where the file resides, if you have the address bar showing).
Still in the Target field, type a space, and then with no quotes, type /decompile
Click OK to accept the changes and to close the dialog box.
Hold the Shift key down and keep it down.
Wiggle your ears
Double-click on the shortcut.
Go to any module in your database.