Deleting a file via AppleScript

3,007 views
Skip to first unread message

F. Alfredo Rego

unread,
Mar 19, 2021, 1:39:26 AM3/19/21
to BBEdit Talk
Hi everyone,

I would like to delete a file via AppleScript.

I’m sure the solution is trivially simple, but so far I can’t find a way.

The easy way would be to do something like this, which sadly does exactly what it says and it doesn’t delete the file:

set theFile to (open file "whatever")
close theFile saving no

I looked around in BBEdit’s dictionary for something naively obvious (perhaps "close theFile deleting yes”) but no such luck.


I would be thankful for any guidance.


Alfredo



MediaMouth

unread,
Mar 19, 2021, 10:39:03 AM3/19/21
to BBEdit Talk
HI Alfredo,

As a one-line command:

tell application "Finder" to delete file "Macintosh HD:Path:To:File"
(* Classic AppleScript colon-delimited file path. *)

tell application "Finder" to delete POSIX file "/Path/To/File"
(* Unix style slash-delimited path. Assumes internal drive* )

tell application "Finder" to delete POSIX file "/Volumes/ExternalVolume/Path/To/File"
(* Allows you to specify any volume *)



Allowing more than one command after the "tell"

tell application "Finder"
delete POSIX file "/Volumes/ExternalVolume/Path/To/File"
(* or any of the other delete flavors above *)
end tell



--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/CA8C47EB-6EBC-4D90-891E-8B7746FCDB23%40gmail.com.

Rich Siegel

unread,
Mar 19, 2021, 10:43:36 AM3/19/21
to BBEdit Talk
On 19 Mar 2021, at 1:39, F. Alfredo Rego wrote:

> Hi everyone,
>
> I would like to delete a file via AppleScript.
>
> I’m sure the solution is trivially simple, but so far I can’t find
> a way.
>
> The easy way would be to do something like this, which sadly does
> exactly what it says and it doesn’t delete the file:
>
> set theFile to (open file "whatever")
> close theFile saving no
>
> I looked around in BBEdit’s dictionary for something naively obvious
> (perhaps "close theFile deleting yes”) but no such luck.

WAYRTTD? The "Close & Delete" command on the File menu will close a text
document and move its backing file to the trash.

R.

--
Rich Siegel Bare Bones Software, Inc.
<sie...@barebones.com> <https://www.barebones.com/>

Someday I'll look back on all this and laugh... until they sedate me.

F. Alfredo Rego

unread,
Mar 19, 2021, 1:46:43 PM3/19/21
to BBEdit Talk
Rich,

What I’m really trying to do is to create a simple AppleScript (which I can invoke via BBEdit) to delete a file.

In the past, for other purposes, I have specified “theFile” via one of these two AppleScript options:

(1)
set theFile to (open file “whatever.txt")

(2)
set filePath to "/Users/alfredo/whatever.txt"
set theFile to POSIX file filePath


There may be other options to specify “theFile”, but I don’t know and I would certainly like to learn about them.


I wish I knew how to do the equivalent of your "Close & Delete”, using some sort of AppleScript syntax:

"Close & Delete" theFile


I suspect that this can be a super-short AppleScript, but unfortunately I have no idea about the syntax.


Thanks.

Alfredo
> --
> This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
> --- You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/07E76664-1A07-4F8F-8775-DE3FD000D65E%40barebones.com.

jj

unread,
Mar 19, 2021, 5:41:46 PM3/19/21
to BBEdit Talk
Hi, Alfredo,

Instead of deleting your file, you should move it to the trash so you can recover it in case of error.

set vFile to POSIX file "/path/to/your/file"

tell application "Finder"

move vFile as alias to the trash

end tell

Best regards,

Jean Jourdain

MediaMouth

unread,
Mar 19, 2021, 5:44:17 PM3/19/21
to BBEdit Talk
tell application "Finder" to delete POSIX file "/Volumes/ExternalVolume/Path/To/File"
act
This will actually put the file in the trash
Then, if you want...

tell application "Finder" to empty trash

F. Alfredo Rego

unread,
Mar 19, 2021, 8:32:52 PM3/19/21
to BBEdit Talk
Hi all,

Thanks for all the help from different perspectives and styles.

In the end, MediaMouth's was the simplest, as a one-liner that abstracted Jean’s multi-liner:

On Mar 19, 2021, at 3:44 PM, MediaMouth <comm...@gmail.com> wrote:

tell application "Finder" to delete POSIX file "/Volumes/ExternalVolume/Path/To/File"
a

Thanks to everyone.

Alfredo

------------------------------------------------

P.S.

Footnote to answer Chris:

My workflow involves copying an updated library from a pre-production (alpha) directory to a production (beta) directory whenever the alpha software passes some tests, which are part of an AppleScript that I run by means of a convenient BBEdit shortcut.

My desired workflow is a work-around for an obvious Automator failing in "Copy Finder Items.

"Copy Finder Items” watches a folder and, whenever a file is ADDED to the folder (see the red rectangle that I used to highlight this spec), it copies the file to a destination folder (with the option to replace existing items).



Unfortunately, "Copy Finder Items” does NOTHING whenever a file is MODIFIED (which is what happens every time that the library gets rebuilt during the compile-link workflow).

So, I need (thanks, Automator ;-) to delete the library BEFORE running the compile-link process, to guarantee that Automator will detect the “ADDITION" of the brand-new library.

An obvious pain.

There are a ton of questions online about the (self-evident) desire to enhance Automator with a Folder action that watches a folder for UPDATED items.

Yes: This entire workflow can be done via a makefile, but other environmental constraints motivate using BBEdit instead.

I learned a lot in the process and I really appreciate that.


Christopher Stone

unread,
Mar 21, 2021, 2:31:04 AM3/21/21
to BBEdit-Talk
On 03/19/2021, at 19:32, F. Alfredo Rego <F.Alfr...@gmail.com> wrote:
In the end, MediaMouth's was the simplest, as a one-liner that abstracted Jean’s multi-liner:
tell application "Finder" to delete POSIX file "/Volumes/ExternalVolume/Path/To/File"
...
Footnote to answer Chris:
...
My desired workflow is a work-around for an obvious Automator failing in "Copy Finder Items.

"Copy Finder Items” watches a folder and, whenever a file is ADDED to the folder (see the red rectangle that I used to highlight this spec), it copies the file to a destination folder (with the option to replace existing items).

<ps 2021-03-19 at 6.18.47 PM.jpg>


Unfortunately, "Copy Finder Items” does NOTHING whenever a file is MODIFIED (which is what happens every time that the library gets rebuilt during the compile-link workflow).


Hey Alfredo,

If you're content with doing this manually then you've got your answer, but I'll add to the plot anyway:



--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/03/21 00:57
# dMod: 2021/03/21 00:57 
# Appl: Finder, System Events
# Task: Handler to Delete a File or Folder Given an:
#     : Alias, HFS-Path, POSIX-Path, $HOME-Based-POSIX-Path, or POSIX-File
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @System_Events, @Delete, @File, @Folder
--------------------------------------------------------

set fileRef to "~/Downloads/TEST_FILE.txt"

deleteItem(fileRef)

--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
# Input: Alias, HFS-Path, POSIX-Path, $HOME-Based-POSIX-Path, or POSIX-File
on deleteItem(fileRef)
    if fileRef starts with "~" or fileRef starts with "/" or fileRef contains ":" then
        tell application "System Events" to set fileRef to (POSIX path of disk item fileRef) as «class furl»
    end if
    tell application "Finder" to delete fileRef
end deleteItem
--------------------------------------------------------

I would just use System Events to delete the item, but it directly deletes – and in general I'd rather toss things in the trash for safety.



If you want to pursue an automated solution, here are a couple of references.





--
Best Regards,
Chris

MediaMouth

unread,
Mar 21, 2021, 3:33:41 AM3/21/21
to bbe...@googlegroups.com
Hi Alfredo,

I wonder if a solution here is rsync.
This is a command line app that can easily be run from AppleScript.  It 'syncs' two directories, a source to a dest (in your case alpha (pre-production) to beta (production))

rsync -avu '/path/to/alpha/directory' '/path/to/beta/directory'

This will update all modified files and add new files from  from alpha to beta.  It will not delete items from beta when items are deleted from alpha.  To achieve that...

rsync -avu --delete '/path/to/alpha/directory' '/path/to/beta/directory'

The added --delete option only affects beta, not alpha

Explainer for -avu options
a: 'archive mode' makes sure things like creation date and mod dates are passed from alpha to beta
v: 'verbose' show a lot of feedback
u: skip files in beta that are newer than those in alpha. (ie saves time)

Ordinarily you do all that in Terminal, which you can call from BBedit.
But you can also run it from AppleScript:

do shell script "rsync -avu '/path/to/alpha/directory' '/path/to/beta/directory'"

Your workflow may have other nuances not captured above, but based on your notes, you might be able to replace an elaborate sequence of Automator and AppleScript with a single line of code easily run from within BBedit

F. Alfredo Rego

unread,
Mar 22, 2021, 11:15:45 AM3/22/21
to BBEdit Talk
Hi “MediaMouth” (sorry about my not knowing your name),

This is perfect.

This makes my Rube-Goldberg approach immediately obsolete.

Your approach cuts away all the unnecessary fat and produces a lean structure.

I’m quickly learning that there’s a command-line app for “that” (whatever “that” may be). The trick (at least for me) is finding out which one applies to what.

Thank you so much for the excellent description.

Alfredo

MediaMouth

unread,
Mar 22, 2021, 12:11:38 PM3/22/21
to bbe...@googlegroups.com
Great.  Glad that worked out.

Peace Keeper

unread,
Mar 22, 2021, 2:51:39 PM3/22/21
to BBEdit Talk
Before I would use rsync from the terminal, you might try out Unison (https://www.cis.upenn.edu/~bcpierce/unison/ ) or RsyncOSX (https://github.com/rsyncOSX/RsyncOSX) which have GUI's to handle bigger directories. - just a thought

F. Alfredo Rego

unread,
Mar 22, 2021, 4:30:32 PM3/22/21
to BBEdit Talk
I’m impressed by all the different approaches and perspectives.

I hope other people have experienced a similar widening and deepening of knowledge regarding the nuances of this topic.

Thanks to everyone!

Alfredo
Reply all
Reply to author
Forward
0 new messages