Error with AppleScript - Integer? - macos

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"

Related

Delete all files in one folder via Apple Script

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.

Apple Script runs more than once when file is opened

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...

Directing an app to run off of a different script in AppleScript.

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.

Applescript Folder Action not working

I wrote an Applescript that would monitor a folder for new input. On receiving the new item, it would open iTunes, create a playlist and add the new files into the playlist. However, when I attach the script as a Folder Action via Folder Action Setup and add a new item, nothing happens. I have placed the script inside /Library/Scripts/Folder Action Scripts as well as ~/Library/Scripts/Folder Action Scripts but it still doesn't fire. Here is my code below:
global album_name
global artist_album
on adding folder items to this_folder after receiving added_items
--get the name of the added folder
repeat with x in added_items
copy name of x as string to playlist_name
set AppleScript's text item delimeters to "-"
set delimited_playlist_name to every text item of playlist_name
set artist_name to text item 1 of delimited_playlist_name
set album_name to text item 2 of delimited_playlist_name
set AppleScript's text item delimeters to ""
end repeat
tell "Finder" to activate "iTunes"
tell application "iTunes"
if not (exists playlist album_name) then
--make iTunes playlist
make playlist with properties {name:album_name}
--add the tracks to the iTunes playlist
repeat with song in playlist_name
add song to playlist album_name
set album of song to album_name
set artist of song to artist_name
set comment of song to ""
set composer of song to ""
set grouping of song to ""
set description of song to ""
set long description of song to ""
set (volume adjustment) of song to 100
end repeat
set view of browser window to album_name
end if
end tell
end adding folder items to
I'm running Mac OS 10.8.4
In the future
Test your code in the Applescript Editor, it helps a lot :)
If you dont use Applescript Editor, to get useful messages, use display dialog entries throughout your code to see how far it goes and what is going on.
So, assuming that the problem is with your code, I rewrote it as an simple Applescript and got it to work on my Snow Leopard (10.6.8) system.
It also runs ok from inside Automator as a Folder Action with the code in a Run Applescript Action item. Once saved from inside Automator it did activate when new files were added to the folder and run ok.
PS: Since you didn't provide any details, I had to guesstimate from your code what you were trying to do. It is far from optimal but it will get you going...
HTH
# remove this line for Applscript Action in Automator...
set input to (choose file with multiple selections allowed)
set playlist_name to {}
#display dialog "Delimeter set to -"
set AppleScript's text item delimiters to "-"
repeat with x in input
#display dialog "Item: " & (x as text)
tell application "Finder"
set fl to name of x
display dialog "Filename: " & fl
end tell
# bring back focus to the editor ## TESTING ONLY
activate me
# append item to the list
copy (fl as text) to the end of playlist_name
display dialog "Playlist count: " & length of playlist_name
set artist_name to text item 1 of fl
display dialog "Artist: " & artist_name
set album_name to text item 2 of fl
display dialog "Album: " & album_name
end repeat
#display dialog "Activating iTunes..."
#activate "iTunes"
#display dialog "iTunes activated!"
tell application "iTunes"
if not (exists playlist album_name) then
display dialog "Making new playlist: " & album_name
make playlist with properties {name:album_name}
display dialog "Changing view..."
set view of browser window 1 to playlist album_name
# add the tracks to the iTunes playlist
repeat with song in input
display dialog "Adding: " & song
set newtrack to (add song to playlist album_name)
set album of newtrack to album_name
set artist of newtrack to artist_name
set comment of newtrack to ""
set composer of newtrack to ""
set grouping of newtrack to ""
set description of newtrack to ""
set long description of newtrack to ""
set (volume adjustment) of newtrack to 100
end repeat
else
display dialog "iTunes playlist already exists!"
end if
end tell

Create selector UI for many applescript's?

i'm newbies with AppleScript and i wanted to create an UI Selector for many of AppleScripts.
Example, i've testA.scpt, testB.scpt, testC.scpt and i want to run just one of these scripts by choosing from a list of choices in a window (UI).
it's possible ?
so, it's possible, could you help me and give an example !?
i think is a very similar code but i'm not sure :
set theName to (choose from list {"testA", "testB", "testC"})
if theName is false then
display dialog "You clicked cancel to exit." with icon stop buttons {"Exit"} default button {"Exit"}
else
set theName to (item 1 of theName)
display dialog theName with icon note buttons {"Info"} default button {"Info"}
end if
Give this a try. It's based on your scripts being in the same directory as this script.
set theName to (choose from list {"testA", "testB", "testC"})
if theName is false then
display dialog "You clicked cancel to exit." with icon stop buttons {"Exit"} default button {"Exit"}
else
set theName to (item 1 of theName)
tell application "Finder"
set the_script_path to ((container of the (path to me)) as text) & theName & ".scpt"
set the_script to load script alias the_script_path
end tell
run script the_script
end if

Resources