Applescript drag and drop doesn't work - drag-and-drop

I am new to Applescript, and wrote a script to securely wipe files, which brings up a file-selection dialog and confirmation, then wipes the file. However, when I tried to add drag and drop functionality, it doesn't work like everyone says it should. The icon never highlights (indicating drag and drop is working), and Finder just copies the file to the app's folder instead of dragging onto the app!
Here is the original script, which works fine (saved as "Wipe File.app"):
on run
set the_file to choose file with prompt "Select the file to wipe:"
wipe_file(the_file)
end run
to wipe_file(file_to_wipe)
set file_to_wipe to POSIX path of file_to_wipe
set ok_to_wipe to display dialog "Are you sure you want to wipe \"" & file_to_wipe & "\"?" buttons {"Cancel", "OK"} default button "Cancel"
set ok_to_wipe to button returned of ok_to_wipe
if (ok_to_wipe = "OK") then
tell application "Terminal"
activate
do script "set prompt='';cls;srm -v \"" & file_to_wipe & "\""
delay 3
close front window
set still_active to count windows
if still_active = 0 then
quit
end if
end tell
end if
end wipe_file
Then I added the following to the top. Running the script still works with the file-selection dialog, but the system never allows drag and drop!
on open the_files
repeat with the_file in the_files
wipe_file(the_file)
end repeat
end open
Everywhere I looked (Google, stack overflow) all say this approach should work, but it doesn't. I even tried removing the on run... block, leaving just on open..., but then the script does nothing at all.
EDIT
If I create a new script with the content above, and save it as an app, then copy it to the Applications folder, and paste a custom icon onto it, drag and drop works. However, the old script, which originally didn't have "on open" support, still doesn't work even after "on open" support was added to the script. So now I'm thinking that Apple must set some special attribute to indicate a script supports drag and drop, and for some reason (because it didn't have it when I first saved?) Apple didn't set that attribute for my file. Looking at Get Info and Show Package Contents for both apps, there are some strange differences:
The newly-created (working) app is only 693 KB, but the old (supposedly identical, but broken) one is 9.4 MB!
Inside the Contents/MacOS folder, the new (working) app has a file called "droplet", whereas the old one (broken) one has a file called "applet".
The first one is bizarre, and made me think of some sort of file corruption, but the second one is clearly the magic setting Apple uses for drag and drop. I verified that if I remove the "on open" block from the working script, and save it, Apple does not update the script icon to remove the "drop" arrow, and you can still drag and drop files onto it, but nothing happens.
So it seems that Apple decides whether a script supports drag and drop or not the first time it saves the script, and after that you get the wrong results unless save a brand new script!

You do not need to call Terminal and open window. The ‘do shell script’ opens already a shell session, in background, not visible in Terminal. You must just use it inside a 'try/end try' block to avoid script stops in case of error (like no authorization for the files !)
Also you are using ‘srm’ with option v (verbose). This option is used to display what is being done, but you do not see it because you are closing the window.
The possible issue of your script could also be when your file path contains special characters which must be escaped in shell. To avoid this, use ‘quoted form of ‘.
Script bellow is tested OK :
on run
set the_file to choose file with prompt "Select the file to wipe:"
wipe_file(the_file)
end run
on open the_files
repeat with the_file in the_files
wipe_file(the_file)
end repeat
end open
to wipe_file(file_to_wipe)
set file_to_wipe to POSIX path of file_to_wipe
set ok_to_wipe to display dialog "Are you sure you want to wipe \"" & file_to_wipe & "\"?" buttons {"Cancel", "OK"} default button "Cancel"
if (button returned of ok_to_wipe = "OK") then
try
do shell script "srm " & quoted form of (file_to_wipe)
end try
end if
end wipe_file

Related

Issues with irrecord lirc function

I am trying to setup a remote to work with an IR reciever that I recently purchased using my raspberry pi, I've installed LIRC according to the following tutorial
https://www.digikey.com/en/maker/blogs/2021/how-to-send-and-receive-ir-signals-with-a-raspberry-pi
now the problem arises when I try and record to setup a specific remote, it saves all the values as 0x0,
here is the console output when setting up the remote that doesn't work
Running as regular user pi
Using driver default on device /dev/lirc0
irrecord: could not open logfile "/home/pi/.cache/irrecord.log"
irrecord: Permission denied
irrecord - application for recording IR-codes for usage with lirc
Copyright (C) 1998,1999 Christoph Bartelmus(lirc#bartelmus.de)
This program will record the signals from your remote control
and create a config file for lircd.
A proper config file for lircd is maybe the most vital part of this
package, so you should invest some time to create a working config
file. Although I put a good deal of effort in this program it is often
not possible to automatically recognize all features of a remote
control. Often short-comings of the receiver hardware make it nearly
impossible. If you have problems to create a config file READ THE
DOCUMENTATION at https://sf.net/p/lirc-remotes/wiki
If there already is a remote control of the same brand available at
http://sf.net/p/lirc-remotes you might want to try using such a
remote as a template. The config files already contains all
parameters of the protocol used by remotes of a certain brand and
knowing these parameters makes the job of this program much
easier. There are also template files for the most common protocols
available. Templates can be downloaded using irdb-get(1). You use a
template file by providing the path of the file as a command line
parameter.
Please take the time to finish the file as described in
https://sourceforge.net/p/lirc-remotes/wiki/Checklist/ an send it
to <lirc#bartelmus.de> so it can be made available to others.
Press RETURN to continue.
Checking for ambient light creating too much disturbances.
Please don't press any buttons, just wait a few seconds...
No significant noise (received 0 bytes)
Enter name of remote (only ascii, no spaces) :notworking
Using notworking.lircd.conf as output filename
Now start pressing buttons on your remote control.
It is very important that you press many different buttons randomly
and hold them down for approximately one second. Each button should
generate at least one dot but never more than ten dots of output.
Don't stop pressing buttons until two lines of dots (2x80) have
been generated.
Press RETURN now to start recording.
................................................................................
Got gap (108172 us)}
Please keep on pressing buttons like described above.
.....................................................................................................................................................Cannot find any gap, using an arbitrary 50 ms one. If you have a
regular remote for e. g., a TV or such this is probably a point
where you hit control-C. However, technical hardware like air
condition gear often works without any gap. If you think it's
reasonable that your remote lacks gap you can proceed.
Press RETURN to continue.
Please enter the name for the next button (press <ENTER> to finish recording)
KEY_UP
Now hold down button "KEY_UP".
Please enter the name for the next button (press <ENTER> to finish recording)
KEY_DOWN
Now hold down button "KEY_DOWN".
Please enter the name for the next button (press <ENTER> to finish recording)
KEY_LEFT
Now hold down button "KEY_LEFT".
Please enter the name for the next button (press <ENTER> to finish recording)
KEY_RIGHT
Now hold down button "KEY_RIGHT".
Please enter the name for the next button (press <ENTER> to finish recording)
KEY_OK
Now hold down button "KEY_OK".
Please enter the name for the next button (press <ENTER> to finish recording)
Checking for toggle bit mask.
Please press an arbitrary button repeatedly as fast as possible.
Make sure you keep pressing the SAME button and that you DON'T HOLD
the button down!.
If you can't see any dots appear, wait a bit between button presses.
Press RETURN to continue.
...Cannot find any toggle mask.
Successfully written config file notworking.lircd.conf
Here is what the config file that's generated looks like
Now, if I setup another remote I have laying around I get a correct config file
Here is a picture of the two remotes sitting side by side, the remote on the left is the one that is not working
I've tried using this tutorial too to no avail
https://www.raspberrypi.org/forums/viewtopic.php?t=235256
does anyone know what's going on here?

Can't edit files in Vscode

I've just started using Vscode and have a created a WebAPI project. On trying to open any of the default code files (Startup.cs and Program.cs) whatever I type starts to appear in the blue bar as shown below instead of where I have placed the cursor. I have no idea what's going on or what that blue bar is.
I am able to add my own code files and edit these as I would expect.
I'm running Vscode 1.29.1 on Win 10 on a Dell Latitude 12 7275. The vscode extension
Enabled extensions :
That's caused by Vim extension. Uninstall or disable it, otherwise use insert key on your keyboard to put it in insert mode.
I'l leave this here for future readers
Basically, if I split files in tabs in VS Code, I could work with the files on the left hand side but could "read-only" all the files on the right hand side.
When I went to the extensions tabs I noticed that whatever I typed was being typed in the field in the extensions search bar. After deleting that, and clicking back on the "Files" tab, it went back to normal and can basically edit files on the both sides of my split screen.
If cannot type
Check where whatever you type is going (e.g. Extensions > Search bar)
Delete wherever it's being typed, and click back on the "Files tab"
Check if it works
I had to change the parent folders permission...
Try changing the entire folder to:
Read & Write: Allows a user to open the item and change it.
https://support.apple.com/guide/mac-help/change-permissions-for-files-folders-or-disks-mchlp1203/mac

play a stream in iTunes without adding it to the library / playlist

I am controlling iTunes via AppleScript and I am playing streams from an HTTP server. The code I'm using looks like this:
tell application "iTunes"
open location "your_url_here"
play
end tell
It works fine, but I would like to avoid those URLs to show up in the iTunes library or any playlist afterwards. Is there a trick to achieve that?
Have you considered using QuickTimePlayer to play the stream instead?
tell application "QuickTime Player"
open URL "http://www.a-1radio.com/listen.pls"
end tell
It should open all the iTunes formats, it comes as a default on OsX, it is more "minimal", won't save tracks.
(I know you specified you want to use iTunes so this might not be a solution, but being preinstalled software it was worth a try)
EDIT To have it hidden, this seems to work:
tell application "QuickTime Player"
-- don't use launch or activate
-- on my mac with launch I see a flicker of the window
set numberOfWindows to (count windows)
repeat with i from 1 to numberOfWindows
close front window
end repeat
end tell
-- may add some delay here if it still flickers the window
-- delay 1
tell application "QuickTime Player"
open URL "http://www.a-1radio.com/listen.pls"
set visible of every window to false
end tell
tell application "System Events"
set visible of process "QuickTime Player" to false
end tell
-- must be called after the open URL command or won't work (no idea why)
To close the stream either quit or close windows (just close if you plan to reopen):
tell application "QuickTime Player"
-- here you can either:
-- close all (will leave app open and hidden)
set numberOfWindows to (count windows)
repeat with i from 1 to numberOfWindows
close front window
end repeat
-- or:
quit -- or just quit (will close app so will need to hide again next time)
end tell
If you hide it before opening the url, it doesn't work (no idea why). Of course the window, even if invisible, is still open so if someone clicks on the dock icon it will show all the open windows.
If you don't want to stop previous streams remove the first repeat -- end repeat part at the top, but leave the set numberOfWindows to (count windows) that is useless but activates the app without needing a activate/launch command

Evaluate live javascript edits in Chrome?

How do I tell the Chrome developer tools that I'm done live-editing a section of javascript, and want the result to be "evaluated"?
Editing the javascript on the fly is simple, I simply put the cursor somewhere in a script in the Scripts tab and start typing, but how do I finish the editing process?
I read here that "changes you apply to the code are expressed only at the time when execution passes through them", but even if I try editing a function when it's being called (i.e., when having a hit breakpoint inside it), I can't get my changes to be acknowledged. Edit: this whole, "execution passes through it" stuff seems false or out of date at best.
Editing javascript on the fly in the developer tools is demoed by Paul Irish in this video around 3:30, but I can't figure out how he goes from editing code, to having it actually be used by the browser.
Ctrl+S or "right-click > Save".
Make sure you haven't enabled pretty-print, as you can't live-edit code, then.
This is what you are looking for:
1.- Navigate to the Source tab and open the javascript file
2.- Edit the file, right-click it and a menu will appear: click Save and save it locally.
In order to view the diff or revert your changes, right-click and select the option Local Modifications... from the menu. You will see your changes diff with respect to the original file if you expand the timestamp shown.
More detailed info here: http://www.sitepoint.com/edit-source-files-in-chrome/
Go to sources, choose the file you want.
Click the pause button to pause debugging .
Modify the script adding a console.log or whatever you want, wherever you want
Save the file (cmd-s or ctrl-2)
Click the pause/resume button to resume execution
the modified code will be executed

Using FileChooserButton in GTK+

I'm trying hard to find out how to use the file chooser button properly. Reading the reference manual on GtkFileChooserButton, GtkFileChooserDialog and GtkFileChooser didn't help so far. I've tried connecting to the undocumented signal file-set of GtkFileChooserButton and calling get\_filename() from here, but it seems to behave quite unreliably. When the user clicks the button and selects a file, it works fine so far.
But, if after clicking on the same button (which already has a filename set), the user changes his mind and closes the "open file" dialog, get\_filename() returns None (a Python type, i write it using pygtk) while the file remains set. Even more weird, if i call get_filename() on that button later from within another signal handler, the filename returns to what it previously had been set by the user. By the way, only closing the dialog box seems to cause the problem, pressing "cancel" works just fine.
Unclear question:
if you mean how to distinct between a valid chosen file:
if you call Run you will get a reponse:
Gtk::RESPONSE_ACCEPT if the user selected a file and accepted this file (clicked ok)
So only get the filename when the file is accepted.
if you mean how to reset filepath:
see gtk FileChooser -> set_ current_folder(filename) (without the space)