I have a piece of code which is used as a folder action and constantly watches the folder. When a new file is placed in the folder, the script runs and outputs a dialog box. The user can either open or print the file from the dialog box. However, when I select the open button, the code then produces the dialog box again when the a new file hasn't been opened. This only occurs when the file is opened and not when it is printed.
Can anyone help? Code is below
on adding folder items to theAttachedFolder after receiving theNewItems
set filepath to theNewItems as string
if filepath contains "HA" then
set theDialogText to "HA is in file name"
do shell script "afplay '/System/Library/Sounds/Submarine.aiff'"
display dialog theDialogText buttons {"Dismiss", "Print", "Go to "} default button "Go to order" with icon note
if result = {button returned:"Go to"} then
tell application "Finder"
open file filepath
end tell
else if result = {button returned:"Print"} then
tell application "Shelf Label Printer"
activate
print filepath
quit
end tell
display dialog "Printed" with icon note
end if
if filepath contains "OG" then
set theDialogText to "OG is in file name"
do shell script "afplay '/System/Library/Sounds/Submarine.aiff'"
display dialog theDialogText buttons {"Dismiss", "Print", "Go to"} default button "Go to order" with icon note
if result = {button returned:"Go to"} then
tell application "Finder"
open file filepath
end tell
else if result = {button returned:"Print"} then
tell application "Shelf Label Printer"
activate
print filepath
quit
end tell
display dialog "Printed" with icon note
Edit: Mojave is running on the iMac in question.
There were a few problematic points in this code, any of which could produce odd behavior, so I cleaned it up so that it works correctly on my machine. New code first, then bullet points on what I changed...
on adding folder items to theAttachedFolder after receiving theNewItems
repeat with thisItem in theNewItems
set filepath to POSIX path of thisItem as string
if filepath contains "HA" or filepath contains "OG" then
set theDialogText to "HA or OG is in file name"
do shell script "afplay '/System/Library/Sounds/Submarine.aiff'"
tell application "System Events"
display dialog theDialogText buttons {"Dismiss", "Print", "Go to"} default button 3 with icon note
end tell
if result = {button returned:"Go to"} then
tell application "System Events"
open file filepath
end tell
else if result = {button returned:"Print"} then
tell application "Shelf Label Printer"
activate
print filepath
quit
end tell
display dialog "Printed" with icon note
end if
end if
end repeat
end adding folder items to
"after receiving" always gives a list of aliases, even if it's a single item, so we should loop through the list rather than trying to convert it directly to string.
You duplicated the same code for 'HA' and 'OG' files, so I merged them
Your Display Dialog cited the default button as "Go to order" when your buttons were "Dismiss", "Print", "Go to". That was throwing an error (Display Dialog wants an exact match to one of the buttons). I replaced that with the index 3.
Folder Actions and the Finder do not always play well together, so I switched to the System Events app for the dialogs and the file open procedure.
This should work properly now...
Related
I worked out two scripts to do that, but none of them is working (I even don't get shown the "on error" message. Maybe it's because of the pre-defined folder as a variable? As for simplicity and a cleaner code I didn't want to write the complete path again, so I used that folder variable. In case it is relevant, the DataHubFolder variable was defined like this earlier: set DataHubFolder to cloudDocs & "Current Projects:Data Hub:"
Here are the codes I used:
Code #1:
try
tell application "Finder"
set f to every file ¬
of folder ¬
of DataHubFolder & "Distrokid:"
delete f
end tell
on error
display dialog ("Error. Couldn't move the file") buttons {"OK"}
end try
Code #2:
try
tell application "Finder"
delete (every item of folder (DataHubFolder & "Distrokid:"))
end tell
on error
display dialog ("Error. Couldn't move the file") buttons {"OK"}
end try
Would be so happy about some tips.
Thanks a lot.
I'm making an applescript to help with school. You type the subject you wish to access, and then it opens that folder (inside the app). However, some subjects have ebooks, so I want there to be another dialog box asking you wether you want to open the ebook or the folder. Unfortunately, a display dialog can't branch off of another display dialog.
So far, the only way I've found around this is to direct the app to run off of another script (in the "Scripts" folder of the app). I've tried
tell application "Script Editor" to run script (path to me as sting) & "Contents:Resources:Scripts:Subject.scpt"`
But it didn't work. Sorry if I'm barking up the wrong tree and seem completely stupid. Thanks
As far as I understood what you're trying to achieve, the following script would work.
set listOfSubjects to {"Math", "Biology", "History", "Languages"}
set subjectsWithEbook to {"Biology", "History"}
choose from list listOfSubjects with prompt "Select the subject"
set subject to item 1 of result
set openEbook to false
repeat with subjectItem in subjectsWithEbook
if subjectItem contains subject then
choose from list {"Open folder", "Open Ebook"} with prompt "What to open?"
set openType to item 1 of result
if openType contains "Open Ebook" then
set openEbook to true
end if
exit repeat
end if
end repeat
if openEbook then
--Open ebook
else
--Open folder
end if
I am guessing that you want to type the subject name? Here is an example that uses dialog boxes.
set subjectFolderPath to (path to me as string) & "Contents:Resources:Subjects:"
set subjectsWithEbook to {"English", "Spanish"}
display dialog "What subject would you like to access?" default answer ""
set theSubject to text returned of the result
if theSubject is in subjectsWithEbook then
display dialog "Would you like to open the Folder or the Book?" buttons {"Folder", "Book"} default button 1
set toOpen to button returned of the result
if toOpen is "Book" then
-- tell app "Preview" to open ???
beep
else
tell application "Finder" to open folder (subjectFolderPath & theSubject & ":")
end if
else
tell application "Finder" to open folder (subjectFolderPath & theSubject & ":")
end if
You might also want to have a list of subjects that are allowed and error check for them.
I am writing a script and want to hem in the user(s) in the future from errors. The big one I'm working on right now is that the Choose File command box for Finder or AppleScript (doesn't matter) do not contain the "giving up after" option. So while I can set the timeout to a very large number of seconds (5000 for example), I can't get the box to close and reopen without the Apple Events timing out.
So here is one option I've tried. but the problem I have is that if I swipe to another screen, even if I Activate the finder, it will say that it can't find the window "Choose a File"
Is there a way to get the window to follow the swipe or a command with activate that will bring the finder window to the current screen, even if I'm working in say Safari?
The error occurs when I swipe to another screen; see the error below:
error "System Events got an error: Can’t get window \"Choose a File\" of process \"Finder\"." number -1728 from window "Choose a File" of process "Finder"
Script:
try
with timeout of 5 seconds
tell application "Finder"
set theFilestoChoose to every item of (choose file with prompt "Please select the file(s) you would like to move and rename:" with multiple selections allowed) as list
end tell
end timeout
on error errStr number errorNumber
if errorNumber is -1712 then --timeout error
my closeWindow() --call handler to close window
end if
end try
on closeWindow()
tell application "System Events"
delay 2 -- for observation testing purposes
set frontmost of process "Finder" to true
delay 2 -- for observation testing purposes
click button "Cancel" of window "Choose a File" of process "Finder"
end tell
end closeWindow
You'll have to look into the defaults setting AutoSwoosh = true; defaults write com.apple.Dock workspaces-auto-swoosh -bool YES ; KillAll Dock which makes you go to the active app, if it isn't in the current space, the app itself, (Finder in this case?), shouldn't be assigned to a space.
If that is your basic configuration, then a simple activate before it, should bring you directly to your choose file dialog, if it is in another space.
Here is a fleshed out example of embedding the choose file with tell application (path to frontmost application as text):
tell application (path to frontmost application as text)
set theF to (choose file)
end tell
I have been making a simple script for my sister, but it does not work. It calls for an integer? I did not specify it in my program, but it calls it anyway...?
My Script:
activate
display dialog "Click Start to start importing your own kindle books!" with title "Kindle Book Uploader by Jeremy Zhang" buttons
{"Cancel", "Start"} default button "Start"
property documentFolder : "documents"
tell application "Finder" to (get name of every disk whose ejectable is true)
try
set kindleLocation to ¬
((choose from list result with prompt "Select your Kindle from the list:") as text)
end try
try
set bookFiles to ¬
((choose file with prompt ¬
"Select kindle files to import:" of type {"public.html", "public.rtf", "com.microsoft.word.doc",
"public.data.mobi", "public.plain-text", "com.adobe.pdf"} with
multiple selections allowed) as text)
end try
display dialog "Please wait while the application copies the kindle books..." with title "Kindle Book Uploader by Jeremy Zhang"
tell application "Finder"
if not (exists folder documentFolder of kindleLocation) then
make new folder at kindleLocation with properties {name:documentFolder}
end if
end tell
set fullKindlePath to POSIX path of (kindleLocation as alias) & "documents"
tell application "Finder"
move (bookFiles) to fullKindlePath
end tell
display dialog "Process has been done! Please eject your kindle and the files will be on the home screen of your Kindle." with title
"Kindle Book Uploader by Jeremy Zhang"
And the result from running it:
tell current application
activate
end tell
tell application "AppleScript Editor"
display dialog "Click Start to start importing your own kindle books!" with title "Kindle Book Uploader by Jeremy Zhang" buttons
{"Cancel", "Start"} default button "Start"
-- {button returned:"Start"}
end tell
tell application "Finder"
get name of every disk whose ejectable = true
-- {"JEREMY DISK"}
end tell
tell application "AppleScript Editor"
choose from list {"JEREMY DISK"} with prompt "Select your Kindle from the list:"
-- {"JEREMY DISK"}
choose file with prompt "Select kindle files to import:" of type {"public.html", "public.rtf", "com.microsoft.word.doc",
"public.data.mobi", "public.plain-text", "com.adobe.pdf"} with
multiple selections allowed
-- {alias "Macintosh HD:Users:JeremyZhang:Downloads:5 ETS SAT S.pdf"}
display dialog "Please wait while the application copies the kindle books..." with title "Kindle Book Uploader by Jeremy Zhang"
-- {button returned:"OK"}
Result:
error "Canât make \"documents\" into type integer." number -1700 from "documents" to integer
What am I doing wrong? Can you please correct me?
AppleScript Editor V: 2.5 (138)
AppleScript 2.2.3
It's a somewhat misleading error. It's telling you that documentFolder is the wrong type. But there are a couple other issues with the script, too. This version should work -- see comments, but try running this as is (I removed the option-return continuation character you were using; I suspect it doesn't translate well on this forum) :
activate
display dialog "Click Start to start importing your own kindle books!" with title "Kindle Book Uploader by Jeremy Zhang" buttons {"Cancel", "Start"} default button "Start"
property documentFolder : "documents"
tell application "Finder" to (get name of every disk whose ejectable is true)
try
set kindleLocation to ((choose from list result with prompt "Select your Kindle from the list:") as text)
--display dialog class of kindleLocation -- class is text
end try
try
set bookFiles to ((choose file with prompt "Select kindle files to import:" of type {"public.html", "public.rtf", "com.microsoft.word.doc", "public.data.mobi", "public.plain-text", "com.adobe.pdf"} with multiple selections allowed) as text)
end try
display dialog "Please wait while the application copies the kindle books..." with title "Kindle Book Uploader by Jeremy Zhang"
tell application "Finder"
--coerce kindleLocation to alias
if not (exists folder documentFolder of alias kindleLocation) then
make new folder at kindleLocation with properties {name:documentFolder}
end if
end tell
--don't use posix ... but you already have variables to build this
--set fullKindlePath to (kindleLocation) & ":documents:"--could've done this, but you don't need this, you can use same construction as above
tell application "Finder"
move (bookFiles) to folder documentFolder of alias kindleLocation
end tell
display dialog "Process has been done! Please eject your kindle and the files will be on the home screen of your Kindle." with title "Kindle Book Uploader by Jeremy Zhang"
I'm trying to enable or disable all the control in a window as the programme changes from interactive to non-interactive mode. How can I ask a window to give me all its contents?
every control of window "mainWindow"
doesn't work, nor does
contents of window "mainWindow"
Actually, I haven't been able to find any good documentation for interacting with menu items from interface builder at all. Things like how to set the contents of popups, and buttons and so on.
thanks
The way I do it at the moment is:
property onlineControls: {"maxLength", "speed", "accelerationSlider", "accelerationField", "showInfo"} --and so on, listing all the controls by name
on enableControls(theList, enableState)
tell window "mainWindow"
repeat with theControl in theList
set the enabled of control theControl to enableState
end repeat
end tell
enableControls(onlineControls, true)
I've made several lists of controls tht get turned on or off depending on the state the programme is in. But it has to be hard coded, which I don't see as being the best way.
tell application "System Events"
tell process "Adium"
get entire contents of window 1
end tell
end tell
This script will give you as result all contents of front window of Adium: butons of window, tool bars of window, buttons of tool bars, etc. Enjoy =]
I haven't been able to find a way to get all the controls in a window, but here's an example of interacting with the menu of a popup button:
tell menu of popup button "somePopupButton" of window "mainWindow"
delete every menu item
repeat with i in someItems
make new menu item at end of menu items ¬
with properties {title:i, enabled:true}
end repeat
end tell
Is the same script as "BoB1990" with the possibility of getting back the information given by get entire contents of window in a string of whom you can observe or modify all the items listed :
tell application "System Events" to tell process "Adium"
set this_info to {}
try
display alert ((get entire contents of window (x as integer)))
on error errMsg set theText to errMsg
set this_info to do shell script " echo " & theText & " | sed 's#System Events got an error: Can’t make ##g;s# into type string.##g'"
end try
set info to {}
set info to do shell script " echo " & this_info
display alert (info)
end tell