I'm trying to do an applescript that pastes data into Numbers and then exports it as a csv file.
It's all working fine except that Numbers keeps my previous documents and opens them every time. This resulting in a new "Untitled" every time the script runs, so if the script is run 10 times - Numbers will open the 10 previous documents first.
I'm trying quit without saving and close this document without saving without results :(.
How can I make Numbers not open old unsaved files everytime Numbers is started?
on run
set theFilePath to "APPLE SSD SMO128B Media:Users:Henrik:Desktop:my.csv"
tell application "Numbers"
activate -- If script is run once before the old document that is exported but not saved will open here too
set thisDocument to make new document
tell thisDocument
delete every table of every sheet
end tell
end tell
tell application "System Events" to keystroke "v" using {option down, shift down, command down}
delay 1
tell application "Numbers"
export front document as CSV to file theFilePath
close thisDocument without saving
delay 1
end tell
tell application "Numbers" to quit without saving
end run
To avoid having Numbers reopening the files you created, you first need to actually save the document to a file, and then ask the Finder to delete it.
You can use the following code:
set documentName to "Test"
set targetFolder to path to downloads folder
set myFile to ((targetFolder as string) & documentName & ".numbers")
tell application "Numbers"
set myDocument to make new document
#Save the file, to delete it just afterward
save myDocument in file myFile
# Export the document
set exportFileName to documentName & ".pdf"
set the targetFilePath to ((targetFolder as string) & exportFileName)
export myDocument to file targetFilePath as PDF
close
quit saving no
end tell
tell application "Finder" to delete file myFile
Related
I have a script that downloads a report from an online service, waits a specified amount of time e.g. 30secs for the file to actually download and then renames the file. The script then repeats
What I would like to do is instead of putting a static delay in the script, to create a trigger that looks for the new file and once it appears triggers the renaming portion of the script.
The downloaded file name is constant.
The current portion of the script looks like this
`
tell application "Finder" to activate
tell application "System Events"
delay Wait_Time
key code 125
tell application "Finder"
set name of (selection as alias) to (the clipboard)
end tell
end tell
ultimately what I would like is to replace the delay Wait_Time with a something like:
repeat until file name found
Search Folder for "file name"
end repeat
select new file
rename new file to clipboard
obviously this wont work but it kind of captures my logic.
I have also tried a few other possible solutions but Im new to AppleScript and not overly confident that these approaches are suitable for my application.
The below is an example of a solution I have tried. This is just an example and im not sure if this particular code can even get me to where I need to be. I thought I would include it to show just how lost I am.
set excludes to {"Folder", "Application"}
tell application "Finder"
set search_folder to folder "Macintosh HD:Users:XXXXXXXX:Library:CloudStorage:OneDrive-Flinders:Flinders Connect Stats:Source Stats:Cisco Finesse:Agent State Summary by Interval Report"
set foundItems to (every item in search_folder whose name contains "Agent State Summary by Interval Report" and kind is not in excludes) as alias list
if foundItems is {} then return
repeat with once from 1 to 100
try
if (count of foundItems) = 1 then
tell application "Finder" to activate
tell application "System Events"
key code 125
tell application "Finder"
set name of (selection as alias) to (the clipboard)
end tell
end tell
exit repeat
end if
end try
end repeat
return
end tell
Im sure you can see I have no idea what im doing.
Anyway any help would be awesome!
I have found a solution... if anyone has ideas for improvement please let me know.
tell application "Finder" to activate
tell application "System Events"
repeat until (exists (files of folder folderPath whose name contains "Agent State Summary by Interval Report"))
delay 1
end repeat
end tell
tell application "System Events"
key code 125
tell application "Finder"
set name of (selection as alias) to (the clipboard)
end tell
end tell
end repeat
say "Finished!"
display dialog "Completed"
The original solution was found here AppleScript: How to repeat a search for a file until it is found?
I need a little help about an AppleScript script which for a folder of pictures :
Make a Selection out of already created path named Path 1
Then Crops the picture by that selection without Deleting the Cropped pixels
I have a problem finding the syntax in AppleScript to turn off "Deleting Cropped Pixels"..and looks like there is no such. I am lookin for a solution to save those pixels and a way to implement it in Applescript.
The solution i have uses key code commands and it's pretty inconvenient because "Delete Cropped Pixels" have to turned off manually in Photoshop before runing the script. Any OS notification outside PS will break the script.
tell application "Finder"
set ThePath to choose folder with prompt "Please select folder:"
set pictures_collection to every file of ThePath
end tell
repeat with anItem in pictures_collection
tell application "Adobe Photoshop CS6"
activate
set anItem to anItem as string
open file anItem
do action "Make_A_Selection" from "Make_A_Selection" -- runs an action with just makes the selection
set OpenedPicture to the current document
tell application "System Events"
-- The option "Delete Cropped Pixels" in the PS Crop tool is turned OFF manually so the next code do not erace the pixels
key code 8 -- press "C"
delay 0.5
key code 36 -- press "enter"
delay 0.5
key code 36 -- press "enter"
end tell
save OpenedPicture
close OpenedPicture
end tell
end repeat
return input
end run
So i looked for more efficient way but unfortunately i don't know how to implement the option not to "Delete Cropped Pixels" in the next script:
on run {input}
tell application "Finder"
set ThePath to choose folder with prompt "Please select folder:"
set pictures_collection to every file of ThePath
end tell
repeat with anItem in pictures_collection
tell application "Adobe Photoshop CS6"'s document 1
activate
set anItem to anItem as string
open file anItem
create selection path item 1
tell application "Adobe Photoshop CS6"
tell current document
set theCropBounds to bounds of selection
crop bounds theCropBounds
end tell
end tell
save OpenedPicture
close OpenedPicture
end tell
end repeat
return input
end run
I've looked in photoshop-cs6-applescript-reference.pdf but as far as i understood i can only set coordinates of the crop.
Any help will be appreciated.
I don't want to delete duplicate files, but I only want to see one instance of each.
Prioritization among duplicates doesn't matter in this case.
///
The actual scenario:
I want to create a VLC playlist containing every video I have ever downloaded from a particular domain. The files are not well-organized and many exist in more than one location on my computer. Consequently, a Finder search for "Where from" --> [the domain] returns many duplicate files. Which means I can't just drag and drop the search results into VLC without there being many duplicates.
I don't want to deduplicate the files themselves.
How can I make this happen?
It is not possible to hide duplicates in the Finder search because there is no option for filtering duplicates in the Finder search.
But your job is to search, then create a playlist. This can be done by dragging and dropping your selection onto the next droplet, which you can call "VLC Playlist Creator". (Or turn it into a "Make VLC Playlist" service using Automator). It will filter for you the duplicates and open Save Playlist Dialog of VLC.app.
on open theAliases
set {namesList, moviesList} to {{}, {}}
repeat with i from 1 to count theAliases
set anAlias to item i of theAliases
tell application "Finder" to set theName to name of anAlias
if not (namesList contains theName) then
set end of namesList to theName
set end of moviesList to anAlias
end if
end repeat
-- try to restart "VLC" to clear old playlist
if running of application "VLC" then quit application "VLC"
repeat while running of application "VLC"
delay 0.1
end repeat
-- add movies to playlist
tell application "VLC"
open moviesList
stop
activate
end tell
-- save playlist dialog
tell application "System Events" to keystroke "s" using command down
tell application "VLC" to activate
end open
I'm a total beginner in apple script.
I would like to open a folder directory that contains hundreds of photos and to view each photo 1 second with quick look and then go to the next (suppose to use down arrow / key code '124').
I don't have any idea of how to build the script. I tried to compile some formulas from other questions or use the manual rec but it doesn't work.
Thanks!
Try following script. I made the delay longer so you have time to stop the script (to test it):
set timePerPreview to 5
set thisFolder to (choose folder with prompt "Pick the folder containing the files to process:") as string
tell application "Finder"
activate
open folder thisFolder
select every item in folder thisFolder
delay 2
set fileCount to (count items in (get selection)) # (count files in folder thisFolder) is faster but counts also .DS_Store and other invisible files
if fileCount = 0 then
beep
return
end if
end tell
pressSpaceInFinder()
delay timePerPreview / 2 # first it has to open the window which seems to need a little time
repeat fileCount - 1 times # -1 because the first item is already displayed
delay timePerPreview
# do shell script "sleep" & space & timePerPreview as text
tell application "System Events"
tell application process "Finder"
set frontmost to true
# cursor right = 124, left = 123
key code 124
end tell
end tell
end repeat
delay timePerPreview
pressSpaceInFinder()
on pressSpaceInFinder()
tell application "System Events"
tell application process "Finder"
set frontmost to true
keystroke space
end tell
end tell
end pressSpaceInFinder
Be aware that it is hard to stop the script since the Finder gets activated every second. Will see if I can add a check to see if the Preview window is open and if not, stop the script.
Also: (without that check) the script will, when the Preview window is open before running, close it but you can just press the space key to open it again.
Also, the first part (getting the file count) isn't so great and may fail when the delay is to short. We could scan the whole folder for images and only count / select those (it has a file template for the scanner, see menu File) but of course you can remove the whole counting thing and just do repeat 1000 times.
I'm trying to automate JPEGmini which is not entirely scriptable - the only way to control it is via the "Open" dialog.
Repeatedly calling it once per image is extremely slow.
Instead I'm trying to to perform a search for .jpg or .jpeg files in the "Open" dialog, so that within that result set I can select the batch of files I want to process and open them in a single pass.
(More detail after the code example)
-- later within the search results list I get, I want to select these
set filesToSelect to {"/Users/me/Desktop/photo.jpg", "/Users/me/Documents/image.jpeg"}
-- the actual app is JPEGmini but the same principle applies to Preview
tell application "Preview"
-- start the app
activate
-- let it boot up
delay 3
-- ensure it still has focus
activate
end tell
tell application "System Events"
tell process "Preview"
-- spawn "Open" window
keystroke "o" using {command down}
delay 1
-- spawn "Go to" window
keystroke "g" using {command down, shift down}
delay 1
-- Go to root of hard drive
keystroke "/"
keystroke return
delay 1
-- Perform search
keystroke "f" using {command down}
delay 1
keystroke ".jpg OR .jpeg"
keystroke return
end tell
end tell
Having done the above, how do I access the list of search results, repeat over each item and get it's absolute file path?
I've tried a few variations but am getting nowhere.
Thanks for your time.
You could simplify this by using cross between the shell command line and applescript.
set jpegSearch to paragraphs of (do shell script "mdfind -onlyin / 'kMDItemContentType == \"public.jpeg\" '")
The mdfind command
consults the central metadata store and returns a list
of files that match the given metadata query. The query can be a string
or a query expression.
AFAIK this is what spotlight uses.
mdfind documents will give you an idea of how this command works. But ths script searches only in "/" and looks for content type attribute that is public.jpeg
The return is text. A list of POSIX Paths of the matching files. This text is like a text document each path on a new line but in effect one item as far as Applescript is concerned.
They need to be broken up in a applescript list. So I do this by asking for the paragraphs of the result.
Also if you want to open a new finder window:
try something like:
tell application "Finder"
activate
set myWindow to make new Finder window to startup disk
end tell
To answer why you are getting your error in trying to get the POSIX paths of the target of the files.
If you look at the properties of one of the files returned in your Search Window.
tell application "Finder" to set fileList to properties of first file of front window
You will see the file properties have a property named original item
If you really want to do it the way you are doing it then one way is to get the original item. Coerce the result into alias form then get the posix path.
tell application "Finder" to set aFile to POSIX path of (original item of first file of front window as alias)
In a normal finder window you can use.
tell application "Finder" to set fileList to POSIX path of (first file of front window as alias)
These are just examples to show you whats going on.
The difference in the type of finder window results is because in a Search window what is being displayed is alias files (class:alias file) to the original files, hence the original item property.
Update 2.
To go through your items in your list and check them against another list is simple.
Apple have some tools that will help you with the code.
When in your script.
crtl + Mouse Click the Variable that will hold the jpg result as a list.
This will give you a contextual menu that contains helper code.
Go to the Repeat routines folder in the menu.
Then to its 'Process Every Item'
This will add a repeat routine to your code.
And from there you can use it to check each item against your other list.
set yourOtherList to {"/Library/Desktop Pictures/Abstract/Abstract 2.jpg", "/Library/Desktop Pictures/Abstract/Abstract 1.jpg"}
set jpegSearch to paragraphs of (do shell script "mdfind -onlyin / 'kMDItemContentType == \"public.jpeg\" '")
repeat with i from 1 to number of items in jpegSearch
set this_item to item i of jpegSearch
if this_item is in yourOtherList then
-- do something
log this_item
end if
end repeat
The repeat routine works like this.
It loops over each item in the given list
It will iterate from item 1 to the count of the items in the list.
The i variable holds the loop iteration number. I.e it will be 1 on the first loop and 300 on the 300th loop.
so on the first loop set this_item to item i of jpegSearch is equivalent to writing set this_item to item 1 of jpegSearch
But apple saves you having to write each number of each item with the Repeat with i..
The variable i can be any word or letter you choose that conforms to the normal allowed Variable naming syntax.
Something you can do is build a new list from the matched items. by copying them into a previously declared list. You can then work on that list after the repeat loop has completed.
Here bigList is declared as a empty list {}
The matched items are copied to the bigList. Each new item it receives is added to the end of the list.
set bigList to {}
set yourOtherList to {"/Library/Desktop Pictures/Abstract/Abstract 2.jpg", "/Library/Desktop Pictures/Abstract/Abstract 1.jpg"}
set jpegSearch to paragraphs of (do shell script "mdfind -onlyin / 'kMDItemContentType == \"public.jpeg\" '")
repeat with i from 1 to number of items in jpegSearch
set this_item to item i of jpegSearch
if this_item is in yourOtherList then
copy this_item to end of bigList
end if
end repeat
bigList