Using hammerspoon and the spaces module to move window to new space - macos

I have installed the "undocumented spaces" module from https://github.com/asmagill/hs._asm.undocumented.spaces. In particular, it provides a method moveWindowToSpace that I am trying to use to bind cmd+1 to move the the current window to space 1 using the following:
local spaces = require("hs._asm.undocumented.spaces")
function MoveWindowToSpace(sp)
local spaceID = spaces.query()[sp]
spaces.moveWindowToSpace(hs.window.focusedWindow():id(), spaceID)
spaces.changeToSpace(spaceID)
end
hs.hotkey.bind({"cmd"}, "1",function() MoveWindowToSpace(1) end)
This works in the sense that it moves the window to a new space, however, the spaces appear to be in a pseudo random order.
Does any one know how to correctly map spaceIDs, as returned by spaces.query(), to the actual spaces?

As undocumented spaces has moved to spaces, the new code would be as follows (some lines could be merged, but I like the clarity of splitting operations):
spaces = require("hs.spaces")
-- move current window to the space sp
function MoveWindowToSpace(sp)
local win = hs.window.focusedWindow() -- current window
local cur_screen = hs.screen.mainScreen()
local cur_screen_id = cur_screen:getUUID()
local all_spaces=spaces.allSpaces()
local spaceID = all_spaces[cur_screen_id][sp]
spaces.moveWindowToSpace(win:id(), spaceID)
spaces.gotoSpace(spaceID) -- follow window to new space
end
hs.hotkey.bind(hyper, '1', function() MoveWindowToSpace(1) end)
hs.hotkey.bind(hyper, '2', function() MoveWindowToSpace(2) end)
hs.hotkey.bind(hyper, '3', function() MoveWindowToSpace(3) end)

After some hints from the author of the spaces module I came up with the following, which seems to do the trick.
local spaces = require("hs._asm.undocumented.spaces")
-- move current window to the space sp
function MoveWindowToSpace(sp)
local win = hs.window.focusedWindow() -- current window
local uuid = win:screen():spacesUUID() -- uuid for current screen
local spaceID = spaces.layout()[uuid][sp] -- internal index for sp
spaces.moveWindowToSpace(win:id(), spaceID) -- move window to new space
spaces.changeToSpace(spaceID) -- follow window to new space
end
hs.hotkey.bind(hyper, '1', function() MoveWindowToSpace(1) end)
Previously I was using a variation on the code at https://github.com/Hammerspoon/hammerspoon/issues/235, which hooks into osx defined hotkeys for switching spaces, but the code above is much faster!

For those in 2020 looking for simpler working solution, you could use apple scripts:
function moveWindowOneSpace(direction)
local keyCode = direction == "left" and 123 or 124
return hs.osascript.applescript([[
tell application "System Events"
keystroke (key code ]] .. keyCode .. [[ using control down)
end tell
]])
end
I tried to use solutions from this issue, but it didn’t work. On the other hand apple scripts work like charm.

Related

VLC Media Player LUA Extension fails detecting whether the media file actually exists or not in Windows

As I stated in Q-title, I am trying to utilize an existing VLC extension developed in LUA programming language.
The Lua extension can be referred from here, when I correctly place this extension in %ProgramFiles%\VideoLAN\VLC\lua\extensions path and then open any video/audio file and run it and then when I select View > Remove current file from playlist and disk option, it closes the currently playing media file & throws this error: lua info: [vlc-delete] error: File does not exist.
Not sure but I suspect this is due to Windows quotes issue when FileName and/or FilePath contains spaces in them. And also from the error, it seems that io.popen("if exist " .. file .. " (echo 1)") : read "*l" == "1" isn't reliable for correctly detecting whether file actually exists or not.
I am relatively new to Lua programming, so can anyone assist about any better methods for checking whether file exists or not that works in latest VLC versions like 3.x+(cause I am using VLC 3.0.17.4 64Bit in Windows 10/11 64Bit), or just assist fix this mentioned issue ?
Note that when the script calls fileExists method, it does takes care of quotes properly: if not fileExists("\"" .. file .. "\"") then return nil, "File does not exist" end
After hours of troubleshooting, I was able to fix the issue, which was due to the way the quoted(space-containing) file path was passed to another function and the Lua library method of os.remove not working on the passed parameter.
So I just had an idea, I am mainly using Windows OS only, then why not convert the script to rely on Windows OS core(batch/cmd) functions only and do the safe checking of whether file exists and then delete with those core Windows functions too.
And so I just did that and got the plugin to actually check whether the file exists and delete the file, if it does then delete it. Here's the working code:
-- Copy this file to %ProgramFiles%\VideoLAN\VLC\lua\extensions\ and restart VLC Media player.
function descriptor()
return {
title = "VLC Delete Media File(Windows only)";
version = "1.0";
author = "Vicky Dev";
shortdesc = "&Remove current file from playlist and disk";
description = [[
<h1>VLC Delete Media File(Windows only)</h1>"
When you're playing a file, use this to easily
delete the current file from your <b>playlist</b> and <b>disk</b> with one click.<br>
Disclaimer: The author is not responsible for damage caused by this extension.
]];
}
end
function sleep(seconds)
local t0 = os.clock()
local tOriginal = t0
while os.clock() - t0 <= seconds and os.clock() >= tOriginal do end
end
function removeItem()
local id = vlc.playlist.current()
vlc.playlist.delete(id)
vlc.playlist.gotoitem(id + 1)
vlc.deactivate()
end
function activate()
local item = vlc.input.item()
local uri = item:uri()
uri = string.gsub(uri, "^file:///", "")
uri = vlc.strings.decode_uri(uri)
path = string.gsub(uri, "/", "\\")
vlc.msg.info("[VLC Delete Media File(Windows only)] removing: "..uri.." : "..path)
removeItem()
retval, err = os.execute("if exist ".."\""..path.."\"".." #(call )")
if (type(retval) == 'number' and retval == 0) then
os.execute("del /f /a /q ".."\""..path.."\"")
end
end
function click_ok()
d:delete()
vlc.deactivate()
end
function deactivate()
vlc.deactivate()
end
function close()
deactivate()
end
function meta_changed()
end
Hope this helps anyone who wants this cool feature of deleting the media files from player itself, instead of going to the location and doing it.

My Roblox Studio script is sometimes having errors.. idk why

So.. I've been coding to make a GUI show the quantity of currency of a player, the datastore API works perfectly but the local script doesn't (it's local because else it would just update it each time a player's currency gets updated and would be a mess being the opposite of what I want to)
and well... sometimes it loads the currency into the GUI but other times it just stays on the original text: "Label" instead of my current currency (4600)
here's the proof
What normally happens and should always happen
What sometimes happens and shouldn't happen:
here's the script, I've tried putting waits on the start but the original code is inside the while true do..
wait(game.Players.LocalPlayer:WaitForChild("Data")
wait(game.Players.LocalPlayer.Data:WaitForChild("Bells"))
while true do
script.Parent.TextLabel.Text = game.Players.LocalPlayer:WaitForChild("Data"):WaitForChild("Bells").Value
wait() --wait is for not making the loop break and stop the whole script
end
well.. if you want to see if data is really in the player, here's the script, it requires a API (DataStore2)
--[Animal Crossing Roblox Edition Data Store]--
--Bryan99354--
--Module not mine--
--Made with a AlvinBlox tutorial--
--·.·.*[Get Data Store, do not erase]*.·.·--
local DataStore2 = require(1936396537)
--[Default Values]--
local DefaultValue_Bells = 300
local DefaultValue_CustomClothes = 0
--[Data Store Functions]--
game.Players.PlayerAdded:Connect(function(player)
--[Data stores]--
local BellsDataStore = DataStore2("Bells",player)
local Data = Instance.new("Folder",player)
Data.Name = "Data"
Bells = Instance.new("IntValue",Data)
Bells.Name = "Bells"
local CustomClothesDataStore = DataStore2("CustomClothes",player)
local CustomClothes = Instance.new("IntValue",Data)
CustomClothes.Name = "CustomClothes"
local function CustomClothesUpdate(UpdatedValue)
CustomClothes.Value = CustomClothesDataStore:Get(UpdatedValue)
end
local function BellsUpdate(UpdatedValue)
Bells.Value = BellsDataStore:Get(UpdatedValue)
end
BellsUpdate(DefaultValue_Bells)
CustomClothesUpdate(DefaultValue_CustomClothes)
BellsDataStore:OnUpdate(BellsUpdate)
CustomClothesDataStore:OnUpdate(CustomClothesUpdate)
end)
--[test and reference functions]--
workspace.TestDevPointGiver.ClickDetector.MouseClick:Connect(function(player)
local BellsDataStore = DataStore2("Bells",player)
BellsDataStore:Increment(50,DefaultValue_Bells)
end)
workspace.TestDevCustomClothesGiver.ClickDetector.MouseClick:Connect(function(player)
local CustomClothesDataStore = DataStore2("CustomClothes",player)
CustomClothesDataStore:Increment(50,DefaultValue_CustomClothes)
end)
the code that creates "Data" and "Bells" is located in the comment: Data Stores
the only script that gets the issue is the short one with no reason :<
I hope that you can help me :3
#Night94 I tryed your script but it also failed sometimes
The syntax in your LocalScript is a little off with the waits. With that fixed, it works every time. Also, I would use an event handler instead of updating the value with a loop:
game.Players.LocalPlayer:WaitForChild("Data"):WaitForChild("Bells").Changed:Connect(function(value)
script.Parent.TextLabel.Text = value
end)

How do I keep a value within QLineEdit to retain its value after program is closed?

I am using Qt Ruby
How do I keep the value of a user entered QlineEdit to keep its state even after the program is closed, in that way, the user can access the contents the next time he opens the program.
Solution 1 - using File.write/read:
edit_widget = Qt::LineEdit.new(parent)
File.write(filename, edit_widget.text)
exit
After the program re-start:
text = File.read(filename)
edit_widget.setText text
Solution 2 - using QSettings:
settings = Qt::Settings.new(filename, Qt::Settings::NativeFormat)
edit_widget = Qt::LineEdit.new(parent)
edit_widget.text = "abcde"
settings.setValue("field1", Qt::Variant.fromValue(edit_widget.text))
settings.sync
exit
After the program re-start:
settings = Qt::Settings.new(filename, Qt::Settings::NativeFormat)
edit_widget = Qt::LineEdit.new(parent)
edit_widget.text = settings.value("field1").toString

Using native Windows FileChooser dialog under PyGTK?

I'm using PyGTK's GtkFileChooserButton, which is working - but looks very weird on Windows environment. Is it possible to use native Windows file chooser dialog?
UPDATE
See the comments for possible directions. However, if you decide (like me...) that it doesn't worth the effort, this will make the Gtk FileChooser more tolerable:
def get_win_my_documents():
# based on http://stackoverflow.com/questions/3858851/python-get-windows-special-folders-for-currently-logged-in-user
# and http://stackoverflow.com/questions/6227590/finding-the-users-my-documents-path
CSIDL_PERSONAL = 5 # My Documents
# the 2 stackoverflow answers use different values for this constant!
SHGFP_TYPE_CURRENT = 0 # Get current, not default value
buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
ctypes.windll.shell32.SHGetFolderPathW(None, CSIDL_PERSONAL, None, SHGFP_TYPE_CURRENT, buf)
if os.path.isdir(buf.value):
return buf.value
else:
# fall back to simple "home" notion
return(os.path.expanduser("~"))
...
my_documents = get_win_my_documents()
chooser.set_current_folder(my_documents)
chooser.add_shortcut_folder(my_documents)
# I'm not sure if it's a general solution, but works for me...
downloads = os.path.join(os.path.expanduser("~"), "Downloads")
if os.path.isdir(downloads):
chooser.add_shortcut_folder(downloads)

Garry's Mod Custom Cvar not changing

I'm making a script for Garry's Mod and it's almost complete but for some reason when ever I type in the new Cvar i made it won't change host_framerate. if anyone know what the problem is your a life saver.
local speedCvar = CreateClientConVar( "speedhack_enabled", 0, true, false )
local speedHackCvar = CreateClientConVar( "speedhack_enabled", "0", true, false )
local speedHack = SpeedHackCvar:GetString()
local speed = function()
if (speedCvar:GetInt() == 1) then
speedHack = SpeedHackCvar:GetString()
RunConsoleCommand("host_framerate", speedHack)
else
speedHack = SpeedHackCvar:GetString()
RunConsoleCommand("host_framerate " , speedHack)
end
end
Remove the local from first of each line (creation of your objects) to make your objects go globally across the lua. Your problem is that your functions are localized, which means they can only be accessed from the file/code block or chunk they're created in.
Oh and also you had a mistake on your code, which Is fixed (From reading comments on your post).

Resources