Applescript open all file in folder one by one - applescript

I'm only a beginner sorry if my question look stupid.
Right now I'm trying to write a script that convert all my .webarchive to .html, so I end up using application called "iCab" to open .webarchive and save it to .html
Here is the question I've all my .webarchive in save folder and I need a loop to open it all one by one, so I can put this fragment of code inside the loop
tell application "iCab" to open file file_name
tell application "iCab" to activate
delay 1
tell application "System Events"
tell process "iCab"
click menu item "Save As…" of menu "File" of menu bar 1
key code 76
click menu item "Close Window" of menu "File" of menu bar 1
end tell
end tell
Thank you in advance

You also need to convert the files from Finder file objects to aliases.
tell application "Finder"
set fl to files of folder POSIX file "/Users/username/Folder/" as alias list
end tell
repeat with f in fl
tell application "iCab"
open f
activate
end tell
tell application "System Events"
tell process "iCab"
click menu item "Save As…" of menu "File" of menu bar 1
keystroke return
click menu item "Close Window" of menu "File" of menu bar 1
end tell
end tell
end repeat

Try:
tell application "Finder" to set myFiles to every file of folder "Mac OS X:Users:Angeloid:Desktop:yourFolder"
repeat with aFile in myFiles
--insert your code here
end repeat
EDIT to include choose folder:
tell application "Finder" to set myFiles to every file of (choose folder)
repeat with aFile in myFiles
tell application "iCab"
open file file_name
activate
end tell
delay 1
tell application "System Events"
tell process "iCab"
click menu item "Save As…" of menu "File" of menu bar 1
key code 76
click menu item "Close Window" of menu "File" of menu bar 1
end tell
end tell
end repeat

Related

Change the way files are sorted on the Desktop with Applescript

I'm trying to write a simple Applescript to change the "Sort by" option in the Finder's Desktop View Options.
When trying to record the action in ScriptEditor, it does nothing. When recording the action with a folder open, it only registers the icon size...
This works for me using the latest version of macOS Mojave.
-- THE NUMBERS IN THE FOLLOWING RECORD VARIABLES ARE KEY CODE NUMBERS
property theSorting : {sortByName:18, sortByKind:19, sortByDateLastOpened:20, sortByDateAdded:21, sortByDateModified:23, sortBySize:22, sortByTags:26}
sortBy(theSorting's sortBySize) -- sort by size
-- sortBy(theSorting's sortByName) -- would be sort by name etc...
to sortBy(theNumber)
tell application "Finder" to activate
repeat until application "Finder" is frontmost
delay 0.1
end repeat
tell application "System Events"
key code theNumber using {control down, option down, command down}
end tell
end sortBy
there can be several ways to do what you want automatically.
with Applescript in the Finder dictionary you can not reach the pop up Name to "show presentation options".
with Automator by creating a service and using "File and Folders" and "get specified items" and "Set Folders View" you can modify all options, and call the service whenever you want.
With Applescript it is necessary to collect UI Element AXSystemFloatingWindow, it is not very digestible, not always effective, but it exists.
Below is an example of applesscrcipt with an already open finder window.
set Name_loc to "Name"
set Name_loc1 to "None"
tell application "Finder"
activate
make Finder window
end tell
tell application "System Events" to tell process "Finder"
set frontmost to true
set wName to name of window 1
if not (exists (first window whose (name is wName and subrole is "AXSystemFloatingWindow"))) then
keystroke "j" using {command down}
repeat
if exists (first window whose (name is wName and subrole is "AXSystemFloatingWindow")) then exit repeat
delay 0.1
end repeat
end if
tell (first window whose (name is wName and subrole is "AXSystemFloatingWindow"))
set thePopUp to pop up button 1
click thePopUp
click menu item Name_loc of menu 1 of thePopUp
set thePopUp to pop up button 2
click thePopUp
click menu item Name_loc1 of menu 1 of thePopUp
click button 1
click button 2
end tell
end tell
tell application "Finder"
activate
quit
end tell
delay 1
tell application "Finder" to activate

How do I refer to an object in a window whose Accessibility Inspector description is "<empty description> (group) [NSBox]" in Applescript?

I am trying to interact with iTunes "Export Library.." dialog.
I tried "set choices to every menu item of menu 1 of pop up button 1 of group 1 of window winName" but it says "group 1" is an invalid index?
Here's the relevant code: (the call parameters are: "iMac-8GB", "iTunes", "iTunes", false"
on handleDir(dir, winName, appName, createIt)
local foundIt, ndx
set foundIt to false
if winName is not "" then
tell application "System Events" to tell process "iTunes"
set choices to every menu item of menu 1 of pop up button 1 of group 1 of window winName
You want to use the menu "Export Library…" from iTunes :
The first step is to select the relevant menu. In my iTunes version (12.5.5.5) the menu is in "File" menu, sub menu item "Library" and then sub Menu "Export Library…".
The second step is to fill the export file name and set the destination folder. The Mac "Save as…" window has many shortcuts, valid for all applications. Among others, command G allows to define the complete path to save the file. This path must be in Unix format (with "/" and not ":" for sub levels).
The bellow script does the complete Export Library function. The first 2 rows define the name of the file to save and the path where to save it. Adjust them to your needs.
set myTitle to "test" -- name of the exported file
set myPath to "/Users/myuser/Desktop/Test_folder" -- destination folder for export file
tell application "iTunes" to activate -- make iTunes front
tell application "System Events"
tell process "iTunes"
click menu 3 of menu bar 1 -- open the File menu
click menu item 12 of menu 3 of menu bar 1 -- select the Library menu item
delay 0.1
click menu item 5 of menu 1 of menu item 12 of menu 3 of menu bar 1 -- select the export library… item
delay 0.1
keystroke myTitle -- fill the export file name in the save as… dialog
keystroke "G" using command down -- shortcut to open Go-to folder window
keystroke myPath
keystroke return -- to close the go-to window
delay 0.1
keystroke return -- to close the export window
end tell -- process iTunes
end tell -- system Events
I added several delays to make sure your Mac has enough time to open or close windows.
Using this code, modified to specifically target iTunes':
tell application "System Events"
tell front window of (first application process whose frontmost is true)
set uiElems to entire contents
end tell
end tell
which came from an answer to Use AppleScript to list the names of all UI elements in a window (GUI scripting)
I discovered that a NSBox is referred to as an "outline" by Applescript.

How to find the Full path to a most recent added file using AppleScript?

I'm using Automator appleScript to upload new pictures to my Instagram.
The script trigger when new file is added to a folder updates
on adding folder items to myFolder after receiving FileList
repeat with aFile in FileList
activate application "Uploader HD for Instagram"
delay 0.5
delay 0.5
tell application "System Events"
tell process "Uploader HD for Instagram"
click menu item "Open..." of menu 1 of menu bar item "File" of menu bar 1
end tell
end tell
delay 1
tell application "System Events"
keystroke "G" using {command down, shift down}
delay 0.5
keystroke myFolder
delay 1
delay 1
keystroke return
keystroke (ASCII character 31)
delay 1
keystroke return
delay 1
keystroke tab
end tell
tell application "System Events"
tell process "Uploader HD for Instagram"
click button "Post" of window "Uploader HD for Instagram"
delay 1
tell application "Uploader HD for Instagram"
quit
end tell
end tell
end tell
try
tell application "Finder"
delete (every item of folder myFolder whose name ends with ".jpg")
end tell
on error
display dialog ("Error. Couldn't Move the File") buttons {"OK"}
end try
end repeat
end adding folder items to
I'm using the var myFolder to search the file using the "Go To the folder"
The var myFolder works but only gives me the path to the folder, and I need the path to the folder + File.ext
Any help will be much appreciated/
Thanks/.
If I well understand what you want to so, you want to open with Uploader HD application each file which is just added into a specific folder.
If this is correct, then just use Folder actions. each time new files will be added to the folder, the system will trigger the script, giving as argument only the files just added (and not the file already stored in that folder previously !). for each file added, you must just ask HD uploader to open it.
You must first assign the script bellow to your folder "Update" :
Copy / paste the script bellow into ScriptEditor and save it in your ~library/Scripts/Folder Action Scripts
Right click on your folder (updates") and add this folder action script.
on adding folder items to myFolder after receiving FileList
repeat with aFile in FileList
tell application "Uploader HD for Instagram" to open aFile
end repeat
end adding folder items to
I can't test with Instagram uploader (I don't have it). It could happen that some applications are not answering to an "open" command. If this is the case you may have to replace the line "tell application...open aFile" by an other method to open it. You can use the variable aFile which contains the complete path to the added file. aFile is an alias type and (aFile as string) is the string which you can use in your goto dialog box.
Here is the final working code, what I was missing is POSIX
The var myFilePath defined with set includes the full path to the file
on adding folder items to myFolder after receiving FileList
try
repeat with i from 1 to number of items in FileList
repeat with aFile in FileList
activate application "Uploader HD for Instagram"
delay 0.5
delay 0.5
tell application "System Events"
tell process "Uploader HD for Instagram"
click menu item "Open..." of menu 1 of menu bar item "File" of menu bar 1
end tell
end tell
delay 0.5
set myFilePath to POSIX path of item i of FileList
tell application "System Events"
keystroke "G" using {command down, shift down}
delay 0.5
keystroke myFilePath
delay 0.5
keystroke return
delay 0.5
keystroke return
delay 0.5
keystroke tab
keystroke "#Instagood #Instagram #Luxury #Babe #Model #Follow #CindyJordanLove"
end tell
tell application "System Events"
tell process "Uploader HD for Instagram"
click button "Post" of window "Uploader HD for Instagram"
delay 1
tell application "Uploader HD for Instagram"
quit
end tell
end tell
end tell
try
tell application "Finder"
delete (every item of folder myFolder whose name ends with ".jpg")
end tell
on error
display dialog ("Error. Couldn't Move the File") buttons {"OK"}
end try
end repeat
end repeat
end try
end adding folder items to

Applescript to "start Dictation" and "stop Dictation" depending on menu

I have on Applescript to start and another one to stop Dictation. I tried to run them together and it only starts Dictation but does not stop it. I tried the key code way but this script is to be used with VoiceOver and that method does not work. Is there a way to have this script check if start/stop is ready and to act accordingly?
tell application "System Events"
tell (get first process whose frontmost is true)
tell menu bar 1
tell menu bar item "Edit"
tell menu 1
click menu item "Start Dictation"
end tell
end tell
end tell
end tell
end tell
tell application "System Events"
tell (get first process whose frontmost is true)
tell menu bar 1
tell menu bar item "Edit"
tell menu 1
click menu item "Stop Dictation"
end tell
end tell
end tell
end tell
end tell
You can use the exists command
tell application "System Events"
tell (get first process whose frontmost is true)
tell menu 1 of menu bar item "Edit" of menu bar 1
if exists of menu item "Start Dictation" then
click menu item "Start Dictation"
else if exists of menu item "Stop Dictation" then
click menu item "Stop Dictation"
end if
end tell
end tell
end tell
--
Or you can use a word in the name of the menu, like this:
tell application "System Events"
tell (get first process whose frontmost is true)
tell menu 1 of menu bar item "Edit" of menu bar 1
click (first menu item whose name contains " Dictation") -- start or stop
end tell
end tell
end tell

Applescript - System events - Set default location for "open file" popup window

Im using system events to control a program that does not have a applescript library.
I am therefor using system events to control it.
I have gotten the program to open a pop up window for it Open File interface and I would like to get it to default to a certain location. Is this possible.
So Far I have :
tell application "App Name"
activate
end tell
tell application "System Events"
tell process "App Name"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
tell menu item "Import"
tell menu "Import"
click menu item "XML..."
delay 4
end tell
end tell
end tell
end tell
end tell
end tell
end tell
The pop up window defaults to its own last visited location. I would like it to default to a given file path like /Users/userabc/Documents/abcd.XML
Thanks
If you have the "posix path" of a location and the dialog box open, you can do the following. Note that the location can be a folder or a file path. If it's a file path then that file will be selected and you would then just have to "keystroke return" to close the dialog box and open that file. Good luck.
set theLocation to path to home folder
set posixLocation to POSIX path of theLocation
tell application "System Events"
keystroke "g" using {command down, shift down}
delay 0.5
keystroke posixLocation
delay 0.5
keystroke return
end tell
The only problem with this method is that autocorrect starts filling in as apple script types into the text box and screws everything up. Work around is to copy/paste into from applescript.
The keystroke command doesn't work for inserting characters that can't be inserted with the current input source. And it doesn't work at all with some input sources.
You could also set the value of the text field:
tell application "System Events" to tell (process 1 where frontmost is true)
keystroke "g" using {shift down, command down}
tell window 1
tell sheet 1
set value of text field 1 to "/usr/share/dict/connectives"
click button 1
end tell
click button "Open"
end tell
end tell

Resources