I am creating a text file and looping through every Safari window, every tab, and pasting each link into the .txt file. However I am getting an extra missing value written to the .txt file. I know I can explicitly check for missing values but I don't understand where is the "empty" window/value?
on run {input, parameters}
(* create text file to hold links *)
tell application "Finder" to make file at desktop with properties {name:"links_0"}
set classicPath to (((path to desktop folder) as string) & "links_0")
(* get links of all windows *)
tell application "Safari"
repeat with this_window in windows
set the_URLs to ""
repeat with this_tab in tabs of this_window
set the_URLs to the_URLs & URL of this_tab & return
end repeat
do shell script "echo " & quoted form of the_URLs & " >> " & POSIX path of (classicPath)
do shell script "echo >> " & POSIX path of (classicPath)
end repeat
end tell
return input
end run
sample output (2 windows, each with 2 tabs):
https://discussions.apple.com/thread/64896
https://discussions.apple.com/thread/22543
https://discussions.apple.com/thread/25140
https://discussions.apple.com/thread/22546
missing value
An easier way to do this would be:
set my text item delimiters to linefeed
set fp to POSIX path of (path to desktop folder) & "links_0.txt"
close access (open for access fp) -- Create the file if it doesn't exist
# set eof of fp to 0 -- Erase any existing contents in the file
tell application id "com.apple.Safari" to tell every tab of every window ¬
to tell (the URL as text) & linefeed to continue write it to fp ¬
starting at eof
This currently appends URLs to the end of the file. To overwrite the file, uncomment this line:
# set eof of fp to 0
by removing the hash symbol.
Note: There's potential for confusion having read the comment left by #user3439894, where he states:
"It also shows code missing from CJK's answer in which a file should be closed after being written to. It also wraps relevant code in a try statement so if there is an error it attempts to close the file."
I would take what you read at the provided link with a pinch of salt. Sadly, a lot of the AppleScript documentation from Apple contains code that is poorly written and is part of the reason a lot of bad AppleScript persists so pervasively on the internet. Some of the documentation provided by Apple is also flat out wrong, which makes it very difficult for people to learn from and to know whose advice to take.
The code I have provided above, I assure you, is very much complete. Read on for a pretty boring explanation that I wish I didn't have to give, but now have to:
You'll see a lot of people do this:
set fh to open for access filepath with write permission
write somedata to fh
close access fh
It's not wrong, per se, but it's a pretty draconian way of using these read/write commands, which persists among many simply because they've always done it that way, and only seen it done that way. It's cumbersome and completely unnecessary1.
#user3439894 alluded to a try statement, which would, indeed, be needed for this method. open for access opens a file handle that was necessary in order to write out to a file. However, if an error in the script occurs during the write process, then the file handle is left dangling because the script terminates before being able to close access to the file. The way around this would be to use a try statement whose purpose was to catch an error, and ensure the close access command still gets executed:
try
set fh to open for access filepath with write permission
write somedata to fh
close access fh
on error
close access fh
end try
open for access and close access are redundant. There's no need to use them, and in fact, their use is only creating a potential problem that then needs a workaround in order to solve.
write--and read--are safe and advisable to use on their own, and they don't need a try block to catch potential errors. So why do the two commands I just called redundant appear in my script at all ?
close access (open for access fp)
One nifty side effect of the open for access command is that it will create a file at the specified path if one doesn't already exist. This is really useful because it negates the need to call out to System Events or (shudder) Finder to do this for you. It also has seems to have a wider scope for creating files at locations that other AppleScript applications don't have permission to access if they're sandboxed, etc.
Once open for access has created the file--or opened one that already exists--it returns a reference to the file handle. Then close access is used to immediately destroy file handle, because we don't actually have any use for it, but also don't want it left open. There's also no need for a try block: any error that could possibly arise would do so during the creation of the file handle, which would mean that the file handle won't be created, and so cannot be left open.
1The way the data gets written out now is fundamentally different to how it was written out when these commands were first introduced, and previously, write would not be able to access a file without first explicitly opening a file handle to it and declaring the need for writing permissions. Now, everything gets handled within the write command itself, including clean up.
Related
I recently put together an AppleScript file which invloves reading a file at one point. The command I am using to do this is:
set paragraph_ores to read file "Macintosh HD:Users:MYUSERNAME:Desktop:ORES.txt" using delimiter linefeed
Now, whilst this works perfectly and as expected, the problem comes that I was hoping to integrate this into a separate application which would run this script when necessary but had the realisation that everybody who would need to use the code would have to go into this script and specifically put in their username which may be tricky or tedious for some people.
In an attempt to circumvent this problem I looked to Google where I found out some more information about HFS file paths to which I had no prior knowledge to find out that the general formula is:
<Volume Name>:<Directory Name>:...:<Directory Name>:<Filename>
And an example they give to use this type of file path is:
Macintosh HD:Applications:Safari.app
This proved confusing as it did not reference the username at all. Therefore I tried changing my original command line to:
set paragraph_ores to read file "Macintosh HD:Desktop:ORES.txt" using delimiter linefeed
However, this proved unsuccessful and I was presented with an error. My next thought was to set a variable to the current username and integrate this into the path as shown below:
tell application "System Events"
set username to name of current user
end tell
set paragraph_ores to read file "Macintosh HD:Users:" & username & ":Desktop:ORES.txt" using delimiter linefeed
Unfortunately, this too, for some reason, seemed not to work.
I would really appreciate it if someone could point me in the right direction of how to solve this problem of avoiding the username in the HFS file path.
Thank you in advance for your help,
Tom
Use the path to (folder) command, e.g.:
set paragraph_ores to read file ((path to desktop as string) & "ORES.txt")
Have a look at path to (folder) in the AppleScript Language Guide, which returns the location of the specified special folder.
I am trying to write an Apple Script for Sketch.app (com.bohemiancoding.sketch3). What i want to do is, create some image file that can be rendered in browser from Sketch document.
And when i open Sketch.app dictionary in Script Editior i see
saveable file format enum
Sketch : The native Sketch 2 file format
PDF : Portable Document Format
TIFF : Tagged Image File Format
So i thought about generating TIFF using following script, but it did not work
tell application "Sketch"
set curdoc to document 0
save curdoc in "/Users/mirza/Downloads/mew2" as TIFF
end tell
I can create sketch copies in .sketch format with save command but not PDF or TIFF. Does sketch supports PDF and TIFF using apple script?
Or is there any other way around for that.
Update
I change the path to apple script format and set document index to 1. Now script looks like this
set thisFilePath to (POSIX file "/Users/mirza/Downloads/mew2")
log thisFilePath
tell application "Sketch"
curdoc to document 1
save curdoc in thisFilePath as TIFF -- Tried with quotes as well, gives same error
end tell
But when i run the script i got the following error
Result:
error "Sketch got an error: Can’t continue curdoc." number -1708
Update 2
Fixed typo
set thisFilePath to (POSIX file "/Users/mirza/Downloads/mew2")
log thisFilePath
tell application "Sketch"
set curdoc to document 1
log (path of curdoc)
save curdoc in thisFilePath as "TIFF"
end tell
But when i run the script i got the following error
Result:
error "Sketch got an error: The document cannot be exported to the \"TIFF\" format." number -50
There are a number of things wrong with your code, but, to start with, you're going to find it hard to get definitive answers using software that isn't available anymore. Sketch has been at version 3 for a while now, and the AppleScript dictionary has probably changed.
That being said, here are some thoughts about your code:
If that is what the Sketch 2 AS dictionary reads, then the AS functionality has changed in v3.
I'd like to help, but I can't find v2 anywhere, so I can only do this in the dark.
set thisFilePath to choose file name--use this to select a new file;
------- a Mac AppleScript path is returned (a file specification,
------- actually, which is different from a string or alias
------- (but an alias is kind of like a file spec)
tell application "Sketch"
set curdoc to document 1--not zero-based; 1 is frontmost doc
save curdoc in thisFilePath as "TIFF"--*this is a guess
end tell
So, I don't know what that last save line will do, but it might work. In Sketch 3, "TIFF" format isn't allowed in saving, but it does have an as parameter as part of the save, which is supposed to be paired with a text string representing the format (like "TIFF", above). Sketch 2 seems to have a different scheme (the parameter with as is not a string). If I save without the as parameter in Sketch 3, it saves in Sketch's native format. So you might try this without quotes (like you have). I'm just doing what the v3 dictionary tells me to do.
Here are a couple of solutions and tips:
document 1 should work to reference the frontmost document;
If you want for some reason to write out your path using POSIX
(like you've done), you can use
POSIX file "/Users/mirza/Downloads/mew2"
to return an AppleScript's Mac-style path, which is of this form:
"yourHardDriveName:Users:mirza:Downloads:new2"
You can also get what I have here as "yourHardDriveHame:" by doing
tell application "Finder" to set sDr to startup disk as string
then concat by doing
sDr & "Users:mirza:Downloads:new2"
You can also do
tell application "Finder" to set myHome to home as string
which should return the Mac-style path to the home folder. (And yes, there are other paths Finder allows you to get, too).
There's some stuff to play with.
I'm trying to create a simple script to delete files based on a custom label I've already assign.
I'm currently trying to limit the search for the script to a test folder, but ultimately I want the script to search in all the user folder and get all the files from several different locations. I may need authentication for the process.
But so far I have this
tell application "Finder" delete (every item of folder
"/users/ro/documents/Erase test" whose label is "test") end tell
and I get this error
error "Finder got an error: Can’t get folder
\"/users/ro/documents/Erase test\"." number -1728 from folder
"/users/ro/documents/Erase test"
As I said I don't really know much about scripts, so I don't know all the terms but I hope someone can point me in the right direction.
Saw this late.
Tested this on 10.6.8 and will jump on a Mavericks machine to test, but this should work:
set f to choose folder
tell application "Finder"
delete (every item of f whose label index is 1)
end tell
A few notes about your attempt:
1) AppleScript doesn't 'natively' understand POSIX paths (but coercion to/from is possible), so (as I have it) "choose folder" returns what is known as an alias (not to be confused with a string -- but again, coercions to/from strings/aliases are simple).
2) note that the label is recognized as "label index", which is an integer.
3) you could/should test by taking out "delete" in that line to return a list of those items.
[edit] yes, this is fine on Mavericks.
Some Background: I've used Skim with BibDesk for a while now for reading and annotating scientific journal articles. Recently, I purchased an android tablet and would like to use it to read and annotate .pdfs as well. I use the reference library Eratosthenes with ezPDF Reader and sync all files through Dropbox. The issue I'm having is that Skim stores annotations as extended attribute files by default, which are not accessible to other devices through Dropbox. I've worked around this problem by saving the annotations as .fdf files and then linking the .fdf files to the citation entry in BibDesk. ezPDF reader can import .fdf files as annotations, and if the annotation file is linked to the BibDesk entry, both the .pdf and .fdf can be easily downloaded to the tablet without needing to sync the entire Dropbox folder full of hundreds of references.
I'd like to write an applescript that does this automatically, but am very new to applescript and am having a hard time getting started. I've written the following script to be executed when "command-s" is pressed while in skim:
tell application "Skim"
set docPath to path of front document
set notesPath to text 1 thru -5 of docPath & ".fdf"
save front document
save front document in notesPath as "Notes as FDF"
end tell
This essentially saves the current document while simultaneously exporting an .fdf file. Within the same script, I would like to link the .fdf file to the appropriate citation entry in BibDesk. This would involve:
determining the citation entry name associated with the .pdf (maybe search through entries to locate one linked with the front document)
check to see if the .fdf file is already linked to it
if not, attach .fdf file
I haven't been able to find someone who's done something similar, and really can't get past the first step. I tried writing something basic (assume citation entry is highlighted, assume .fdf file is not linked), which produces no results:
tell application "BibDesk"
set thePub to selection
tell thePub
set theFieldName to "Local-URL-2"
set value of field theFieldName to fdfPath
end tell
end tell
Is anyone familiar with Bibdesk applescripts able to help me with the second part of this code?
Thank you very much in advance.
I'm not too familiar with scripting BibDesk, but I've been poking around a little. Heavily commented to help guide you:
set theFieldName to "Local-URL-2"--generally better not to put
--something like this in a tell block if it isn't necessary
tell application "BibDesk"
--as you have it, you are simply putting the selection class
--into a variable. here we reference the specific selection
--object of the document object:
set thePubSel to selection of document 1
--and, since this returns a *list* of pubs, I'm grabbing just the first item
--(but a loop iterating through every publication in the selection perhaps better)
set thePub to item 1 of thePubSel
tell thePub
set value of field theFieldName of it to fdfPath
end tell
end tell
The following code answers my initial question satisfactorily. The first section reiterates the saving and exporting commands. The second section locates the citation entry containing the linked .pdf file (front document in Skim). The third section attaches (links) the .fdf file to the citation entry (thanks to CRGreen for some help here).
--save and export .fdf file
tell application "Skim"
set docPath to path of front document
set fdfPath to text 1 thru -5 of docPath & ".fdf"
save front document
save front document in fdfPath as "Notes as FDF"
end tell
--search for relevant citation entry
tell document 1 of application "BibDesk"
--sort all publications in library by Cite Key
set thePubs to (sort (get publications) by "Cite Key")
-check each publication individually (surely this is not the most efficient way to do this)
repeat with aPub in thePubs
--check to see if the .pdf is in the citation entry
tell aPub
if linked files contains (POSIX file docPath) then
set thePub to aPub
--once the citation is found, exit loop
exit repeat
end if
end tell
end repeat
--link the .fdf file to the citation entry (if it isn't already)
tell thePub
--if the fdf file exists in the linked file, do nothing
if linked files does not contain (POSIX file fdfPath) then
add (POSIX file fdfPath) to end of linked files
end if
end tell
end tell
I'm assuming that there is a better way to search for the citation entry with the associated .pdf file (maybe using the applescript search command?). This solution works for me, but if you know of a more elegant way to solve this problem, feel free to mention it.
To map the script to a keyboard shortcut ("command-s" is convenient), see here (the menu title is the name of your script). The script needs to first be saved to the ~/Library/Application Support/Skim/Scripts directory.
I'm just learning applescript, and I realize that this may have been a very trivial exercise for most, but perhaps it will help out another beginner.
I'm starting to poke around with Applescript and am looking at writing a few scripts for managing windows. A common task they will all need is to get the current screen size.
I've created a screen_size subroutine that seems to work, and I want to be able to share that with all my scripts. However, I can't figure out a way to put that in a separate file that I can load in my other scripts. I tried creating a separate screen_size.scpt file and use load script "screen_size.scpt", but I get an error about "can't make "screen_size.scpt" into a type file".
There has to be a way to do this, but I haven't been able to find anything online about how to do it.
EDIT:
The POSIX stuff suggested isn't working for me. I'm able to create the file object, but it refuses to convert to an alias, saying it can't find the file (looks like the POSIX file stays relative instead of expanding fully).
I found a suggestion online to use Finder, and have gotten the following working to get an alias:
tell application "Finder"
set _myPath to container of (path to me) as text
end tell
set _loadPath to (_myPath & "screen_size.scpt")
set _loadAlias to alias _loadPath
However, the next line fails with a syntax error, claiming that _loadAlias isn't a variable:
property _ScreenSize : load script _loadAlias
Every variation of this I've tried (doing the alias in the load call, etc) fails, always claiming the variable doesn't exist, even though I know it's being set and working as I can display it. What's going on? Why is it claiming a variable doesn't exist when it obviously does?
AppleScript is doing some really weird things when saving and I haven't figured out what's going on, but I ended up getting something to work.
Here's what I have:
on load_script(_scriptName)
tell application "Finder"
set _myPath to container of (path to me) as text
end tell
set _loadPath to (_myPath & _scriptName)
load script (alias _loadPath)
end load_script
set _ScreenSize to load_script("screen_size.scpt")
set _bounds to _ScreenSize's screen_size()
-- ...
The other answers were saying to set _ScreenSize as a property, but that would cause a syntax error which prevented me from ever saving the file. When I did it just using set it worked.
I wasn't ever able to get the POSIX path stuff suggested to work, but poking Finder for the path worked fine.
In order to execute an action from another script, you'll have to create an handler in the script you're going to load (in your answer you already did this with "screen_size()".
In your case this script will be "screen_size.scpt".
So "screen_size.scpt" will have to look something like this:
on screen_size()
--your actions
return [yourvalue] --the value you want to pass to the other script
end screen_size()
The script you'll load it from will have to look like this:
tell application "Finder"
set _myPath to (container of (path to me) as text & "screen_size.scpt") as alias
end tell
set _ScreenSizeScript to load script _myPath
set _bounds to _ScreenSizeScript's screen_size()
If it doesn't work, or you don't understand me completely, feel free to ask (:
Yes there is a way to do this. Load the file into a property and access it that way
property _ScreenSize : load script (alias "pathtoscript")
_ScreenSize's doStuff()
and for relative paths try this:
set p to "./screen_size.scpt"
set a to POSIX file p
so perhaps this will work:
set p to "./screen_size.scpt"
set a to POSIX file p
property _ScreenSize : load script (alias a)
_ScreenSize's doStuff()
I have people using my libraries on a daily basis, so I first ensure the library is here before calling it.
Let's say I have a library "Lib.Excel.app" (save as non-editable application with Satimage's Smile).
At the beginning of a script that makes use of it, I "load" the library by using this code :
set commonCodeFile to (path to library folder as string) & "Scripts:CommonLibraries:Lib.Excel.app"
tell application "Finder"
if not (exists (file commonCodeFile)) then error ("\"Lib.Excel\"
" & "
should be found in folder
" & "
scroll > CommonLibraries")
end tell
global cc -- make it short and easy to write :)
set cc to load script alias ccFile
Then when I have to use a function from the lib, I just call it like this :
set {what, a} to cc's veryNiceFunction()
Yes you can. You need the full path to the script however.
I believe you can still use "path to me" to get the path to the app executing the current script, and you can then modify that path to point to your sub-folder containing the scripts.
I used this technique to get around AppleScripts (former) 32k text size limits years ago for some really large/complex IRC scripting.
I think I still have all those old scripts in my G4, which is under the desk in my office at work. Sadly it's behind a Enet switch and I can't VNC into it otherwise I'd have tons of sample code to post.
You CAN load the script in a variable, but you have to declare it first.
property _ScreenSize : missing value
tell application "Finder" to set _myPath to container of (path to me) as text
set _loadPath to (_myPath & "screen_size.scpt")
set _loadAlias to alias _loadPath
set _ScreenSize to (load script _loadAlias)